I want to be able to dispatch to methods defined within an environment
. This would allow me to define polymorphic methods for proto
objects (proto
objects in turn inherit from the environment
class). E.g.:
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!"
And so I can do:
> x$foo(list())
[1] "obj is a list!"
> x$foo(1)
[1] "obj is neither a list nor a data.frame!"
However, all I get now when calling x$foo
is
Error in UseMethod("foo") :
no applicable method for 'foo' applied to an object of class "c('proto', 'environment')"
How to fix this?
I do not know how proto works, but as for environments, it works:
proto seems to override the $ operator, so you may hack it like this: