1

我正在寻找一种方法来告诉引用类的实例忘记它的一个方法定义。例如,我创建了一个类MyReferenceClass和一个名为的实例,my_object我可以调用该方法print_hello并且一切正常:

MyReferenceClass <- setRefClass("MyReferenceClass",
    methods = list(
        print_hello = function(){
            print("hello")
        }
    )
)
my_object <- MyReferenceClass$new()
my_object$print_hello() # "hello"

如果我通过添加新方法 ( print_goodbye) 来更新类定义,我现有的对象将能够使用它。但是,如果我更改之前定义的方法 ( print_hello),它不会更新:

MyReferenceClass <- setRefClass("MyReferenceClass",
    methods = list(
        print_hello = function(){
            print("hello_again")
        },
        print_goodbye = function(){
            print("goodbye")
        }
    )
)
my_object$print_goodbye() # "goodbye" => it works
my_object$print_hello() # "hello" => it doesn't work

有没有办法告诉my_object忘记它的定义print_hello?这不起作用:my_object$print_hello <<- NULL

4

2 回答 2

3

AFAIK 的答案是否定的,当试图“事后”通知对象类定义更改时,即在它被实例化/创建之后。

一旦您创建了 S4 类的实例,该对象就会像您创建对象时一样“绑定”到类 def。在我看来,这是完全有道理的。不确定以前缺少的方法(即print_goodbye())的“成功”更新是“偶然”起作用还是实际上是所需的行为。

处理更新的类定义的推荐方法

我的建议:如果您决定想要/需要更新您的类定义,那么重新采购整个项目代码会更安全。这样,您可以在创建实际实例之前确保一切就绪。我认为其他任何东西都是非常摇摇欲坠的黑客行为。

如果你还是决定破解

可能有一些肮脏的方法来破解.refClassDef实际包含类 def 的引用类实例的隐藏对象字段(请参阅 参考资料my_object$.refClassDef)。但是设置此字段(即<-在其上使用)不起作用:

my_object$.refClassDef <- MyReferenceClass
Error in envRefSetField(x, what, refObjectClass(x), selfEnv, value) : 
  '.refClassDef' is not a field in class "MyReferenceClass"

也没有通过以下方式进行显式分配assign()

assign(".refClassDef", MyReferenceClass, my_object)
Error in assign(".refClassDef", MyReferenceClass, my_object) : 
  cannot change value of locked binding for '.refClassDef'

更深入的破解可能涉及查看attributes(my_object$.refClassDef). 在那里,您可能会找到构成 ref 类 def 的实际部分。但是,我不知道是否即使改变任何东西都会“立即”反映出来。

此外,resetClass()可能会给你一些更多的见解。


更新:2014-03-19

为了处理您的缓存方法,我想到了两种方法:

1.最明显的方式:使用copy()

?setRefClass

MyReferenceClass <- setRefClass("MyReferenceClass",
    methods = list(
        print_hello = function(){
            print("hello")
        }
    )
)
my_object <- MyReferenceClass$new()
MyReferenceClass <- setRefClass("MyReferenceClass",
    methods = list(
        print_hello = function(){
            print("hello_again")
        },
        print_goodbye = function(){
            print("goodbye")
        }
    )
)

复制前:

my_object$print_hello()
[1] "hello"

复制后:

my_object <- my_object$copy()
my_object$print_hello()
[1] "hello_again"

2. 黑客攻击attributes(my_object$.refClassDef)$refMethods(大纲,还没有工作)

即使我不建议真正依赖这样的东西,hacks 始终是深入了解事物如何工作的好方法。

在这种情况下,我们可以尝试修改attributes(my_object$.refClassDef)$refMethods包含实际方法 defs 的环境,因为我猜这是调用方法时对象“看起来”的地方。

覆盖实际的方法 defs 没有问题,但它似乎没有立竿见影的效果。我猜想涉及的“旧”类定义有更多“链接”,需要以类似的方式手动更新。

请注意,它my_object仍然具有print_hello打印的方法"hello"

attributes(my_object$.refClassDef)$refMethods$print_hello
Class method definition for method print_hello()
function () 
{
    print("hello")
}

这就是覆盖函数的样子:

ensureRecentMethods <- function(obj, classname) {
    ## Get generator //
    gen <- getRefClass(classname)

    ## Get names of methods belonging to the class of 'obj' //
    ## This will serve as an index for the update
    idx1 <- names(Filter(function(x) {attr(x, "refClassName") == class(obj)}, 
        as.list(attributes(obj$.refClassDef)$refMethods))
    )
    #idx2 <- names(Filter(function(x) {attr(x, "refClassName")==gen$className}, 
    #    as.list(gen$def@refMethods)
    #))
    ## Note:
    ## 'idx2' could be used to enforce some validity checks such as 
    ## "all old methods must also be present in the updated class def"

    ## Overwrite //
    for (ii in idx1) {
        ## Note how we are overwriting the old method defs in environment
        ## 'attributes(obj$.refClassDef)$refMethods' with the updated 
        ## definitions taken from the generator of the updated class 
        ## 'gen$def@refMethods[[ii]]' by making use of the index retrieved 
        ## one step before ('idx1')
        expr <- substitute(
            assign(x=X, value=VALUE, envir=ENVIR),
            list(
                X=ii,
                VALUE=gen$def@refMethods[[ii]],
                ENVIR=attributes(obj$.refClassDef)$refMethods
            )
        )
        eval(expr)
    }

    ## As at the end of the day ref class objects are nothing more than 
    ## environments, there is no need to explicitly return the actual 
    ## ref class object 'obj' as the original object has already 
    ## been updated (pass-by-reference vs. pass-by-value)
    return(TRUE)
}

应用它:

ensureRecentMethods(obj=my_object, classname="MyReferenceClass")

即使 defprint_hello确实被覆盖了,该对象仍然以某种方式获取“旧”版本:

attributes(my_object$.refClassDef)$refMethods$print_hello 
## Note the updated method def!

Class method definition for method print_hello()
function () 
{
    print("hello_again")
}

my_object$print_hello()
[1] "hello"
于 2014-03-18T23:00:05.640 回答
1

Take advantage of my_class@generator$def@refMethods

How about including an update method in the original class, as did here,

Manual modifications of the class definition of a Reference Class instance

于 2014-04-03T17:55:01.550 回答