Reputation: 13080
Is there a way to list only those methods of a Reference Class, that were explicitly defined in the class definition (as opposed to those methods inherited by "system classes" such as refObjectGenerator
or envRefClass
)?
Example <- setRefClass(
Class="Example",
fields=list(
),
methods=list(
testMethodA=function() {
"Test method A"
},
testMethodB=function() {
"Test method B"
}
)
)
What you currently get by calling the $methods()
method (see ?setRefClass
):
> Example$methods()
[1] "callSuper" "copy" "export" "field" "getClass"
[6] "getRefClass" "import" "initFields" "show" "testMethodA"
[11] "testMethodB" "trace" "untrace" "usingMethods"
What I'm looking for:
> Example$methods()
[1] "testMethodA" "testMethodB"
Upvotes: 4
Views: 107
Reputation: 269634
1) Try this:
> Dummy <- setRefClass(Class = "dummy")
> setdiff(Example$methods(), Dummy$methods())
[1] "testMethodA" "testMethodB"
2) Here is a second approach which seems to work here but you might want to test it out more:
names(Filter(function(x) attr(x, "refClassName") == Example$className,
as.list(Example$def@refMethods)))
Upvotes: 3
Reputation: 103898
No, because the methods in a reference class "inherited" from the parent are actually copied into the class when it is generated.
setRefClass("Example", methods = list(
a = function() {},
b = function() {}
))
class <- getClass("Example")
ls(class@refMethods)
#> [1] "a" "b" "callSuper" "copy" "export"
#> [6] "field" "getClass" "getRefClass" "import" "initFields"
#> [11] "show" "trace" "untrace" "usingMethods"
But you can find out the method also defined in the parent and return those:
parent <- getClass(class@refSuperClasses)
ls(parent@refMethods)
#> [1] "callSuper" "copy" "export" "field" "getClass"
#> [6] "getRefClass" "import" "initFields" "show" "trace"
#> [11] "untrace" "usingMethods"
(note that I'm ignoring the possibility that your class has multiple parents, but that would be easy to generalise)
And then use setdiff()
to find the difference
setdiff(ls(class@refMethods), ls(parent@refMethods))
#> [1] "a" "b"
Upvotes: 2