2012-10-28 1 views
5

필드 중 하나를 잠근 적이없는 경우 R5 참조 클래스를 복사 할 수 있지만 필드 중 하나가 잠겨 있으면 복사되지 않습니다. 예제 코드는 다음과 같습니다 (잠금 호출이 주석으로 처리됨). 내 질문 : copy() 메서드를 사용하여 잠긴 필드가있는 인스턴스의 복사본을 만들 수없는 이유는 무엇입니까? 인스턴스를 복사 할 때, 필드 데이터가 잠금에 의해 금지되어 두 번가 할당되기 때문에잠긴 변수가있는 R5 참조 클래스 복사

example <- setRefClass('example', 
    fields = list(
     count = 'numeric', 
     data = 'data.frame', 
     d.accessor = function(x) { 
      if (!missing(x)) 
       data <<- x 
      else 
       .self$data 
     } 
    ), 
    methods = list(
     initialize = function(data) { 
      if (!missing(data)) 
       d.accessor <<- data 
      count <<- 0 
     }, 
     finalize = function() 
      print('Bye Bye'), 
     accumulate = function(x) 
      count <<- count + x 
    ) 
) 

#example$lock('data') # write-1, read-many 
instance <- example$new() # instantiation 
df <- data.frame(x=1, y=2)# example df 
instance$d.accessor <- df # 1st set - okay! 
copyInst <- instance$copy() 

답변

1

때문이다. 이 copyInst$datainstance$data 값을 복사하여 먼저 할당 할 것이다 초 때 복사 copyInst$d.accessorinstance$d.accessor, d.accessor이 게터/세터, 그리고 그것을 $data에 할당 결과에 할당 당신이 그것을 정의하는 방식이 때문이다.

그림 :

example$lock('data') # write-1, read-many 
instance <- example$new() # instantiation 
df <- data.frame(x=1, y=2)# example df 
instance$data <- df # 1st assignement: OK 
instance$d.accessor <- df #snd assignemnt: ERROR 
Error: invalid replacement: reference class field ‘data’ is read-only