我希望能够分派给environment
. 这将允许我为proto
对象定义多态方法(proto
对象又从environment
类继承)。例如:
x <- proto()
x$foo <- function(., obj) UseMethod("foo", obj)
x$foo.list <- function(., obj) "obj is a list!"
x$foo.data.frame <- function(., obj) "obj is a data.frame"
x$foo.default <- function(., obj) "obj is neither a list nor a data.frame!"
所以我可以这样做:
> x$foo(list())
[1] "obj is a list!"
> x$foo(1)
[1] "obj is neither a list nor a data.frame!"
但是,我现在打电话时得到x$foo
的只是
Error in UseMethod("foo") :
no applicable method for 'foo' applied to an object of class "c('proto', 'environment')"
如何解决这个问题?