For a given reference class method, how can I determine if it is inherited? More generally, how to determine how far up the inheritance tree I am?
For example, if my setup is:
A <- setRefClass("A",
methods = list(foo = function() whosMethod())
)
B <- setRefClass("B",
contains = "A",
methods = list(bar = function() whosMethod())
)
b <- B()
ideally, I want whosMethod()
to give me something like
> b$foo()
[1] "A" # or maybe a numeric value like: [1] 1L
> b$bar()
[1] "B" # or maybe a numeric value like: [1] 0L
Note this is distinctly different from class(.self)
, which would always return "B"
in the above example.
Motivation - custom events
I want to have inheritance-like behavior for other things besides methods, for instance custom events. My methods may raise(someEvent)
and during instantiation I pass event handlers to handle those events, e.g.
MyDatabase <- setRefClass(....)
datasourceA <- MyDatabase(....,
eventHandlers = list(
someEvent = function() message("Hello from myObj!"),
beforeInsert = function(data) {
if (!dataIsValid(data))
stop("Data is not valid!")
}
)
)
Now, if a child class defines a event handler that has already been defined by a parent class, then I need to know which event handler should be overridden. In particular, if a methodA()
registers handlerA()
for someEvent
and methodB()
in a child class registers handlerB()
for the same event, when attempting to register handlerA()
in methodA()
I need to know that I am in an parent method so that if handlerB()
is already registered, I do not override it.
It would also be nice to be able to call parent event handlers from child ones, like callSuper()
available to methods.