2

I'm using setMethodS3 in package R.methodsS3 to create an S3 method. Lets say I have two classes, class Parent and class Child (R.oo object). class Child inherits from class Parent. Both have the method MyMethod(). How do I call superclass MyMethod() (Parent's MyMethod) from Child's MyMethod()? I tried this$MyMethod(), but it calls Child's MyMethod()

Here's a reduced example:

library( R.oo )

setConstructorS3( "Parent" , definition = 
function()
{
    extend( Object() , "Parent" , .stateVar1 = FALSE )
} )

setMethodS3( "MyMethod" , "Parent" , appendVarArgs = FALSE , definition = 
function( this , someParam , ... )
{
   print( this$.stateVar1 )
   print( someParam  )
} )

setConstructorS3( "Child" , definition = 
function()
{
    extend( Parent() , "Child" )
} )

setMethodS3( "MyMethod" , "Child" , appendVarArgs = FALSE , definition = 
function( this , someParam , ... )
{
   NextMethod( "MyMethod" ) # does not work
   this$MyMethod( someParam ) # also does not work
} )

child = Child()
child$MyMethod()
Argalatyr
  • 4,639
  • 3
  • 36
  • 62
Suraj
  • 35,905
  • 47
  • 139
  • 250
  • Thanks to Henrik for a workaround which we discussed in email: MyMethod.Parent(this, someParam) – Suraj Nov 30 '11 at 22:40

2 Answers2

1

You do want to use NextMethod() to achieve this. NextMethod() will work if you use MyMethod(child), which I strongly recommended.

The fact that is does not work with the child$MyMethod() seems to be a bug of the Object class. I'll look into it. I think this bug has passed unnoticed because the <object>$<method>() construct is so rarely used by anyone. The MyMethod(child) construct is standard R. We use it in all our code (>100,000 lines). Honesty, I wish I never wrote about child$MyMethod() in the R.oo paper (2003).

Finally, although not required, I recommend that you use the RCC convention using CapitilizedNames for classes and nonCapitalizedNames for methods and objects, setMethodS3("myMethod", "Child", ...).

/Henrik (author of R.methodsS3 and R.oo)

HenrikB
  • 6,132
  • 31
  • 34
  • Henrik - I'm surprised that $() is used rarely; it seems more intuitive for users with an OO background. It would be great to see a bug fix for this. All of my code is in object$method format and its a big impact to change it. I imagine there are others who took this convention, but aren't calling superclass methods. In the basic case of creating an object with no super/sub class, R.oo still provides great productivity gains. I bet there others using R.oo just for atomic objects. It doesn't seem right that $() is lesser than method(object). Just my opinion – Suraj Nov 29 '11 at 21:39
  • ...by the way, I think your R style/coding convention guide is awesome! I wrote my own because I was not satisfied with the Google guidelines and then I stumbled upon yours and was relieved to see that we agree on most topics. camelCase for methods has never agreed with me, particularly because its not the convention in other languages that I code in. I use camelCase for parameter names, so myMethod( myParam ) does not visually distinguish the method from the parameters enough for my tastes. – Suraj Nov 29 '11 at 21:42
0

Is there a reason you don't want to use

MyMethod(child)

instead of

child$MyMethod()
Max Gasner
  • 1,191
  • 1
  • 12
  • 18
  • Hi Max - not sure how the distinction between the two resolves the issue of calling overloaded method in superclass? I use child$MyMethod() because it more closely matches other object-oriented languages and I find it more readable. – Suraj Nov 29 '11 at 03:47