[R] Question on callNextMethod
cberry at tajo.ucsd.edu
cberry at tajo.ucsd.edu
Tue Nov 6 17:16:00 CET 2012
Simon Knapp <sleepingwell at gmail.com> writes:
> I don't understand why I get the following results. I define two classes
> 'Base' and 'Derived', the latter of which 'contains' the first. I then
> define a generic method 'test' and overload it for each of these classes. I
> call 'callNextMethod()' in the overload for Derived. From the output, it
> appears that the overload for Base gets called twice. Why is this? Test
Autoprinting has tricked you.
> a <-test(d) # What you expected
[1] "derived\ncalled"
[1] "base called"
> a # what was autoprinted
[1] "base called"
>
HTH,
Chuck
> code follows:
>
> setClass('Base')
> setClass('Derived', contains='Base')
> setGeneric('test', function(x) standardGeneric('test'))
> setMethod('test', signature(x='Base'), function(x) print('base called'))
> setMethod('test', signature(x='Derived'), function(x) {print('derived
> called'); callNextMethod()})
>
> d = new('Derived')
> test(d)
>
>
> Produces the output:
>
> [1] "derived called"
> [1] "base called"
> [1] "base called"
>
>
> and I was expecting:
>
> [1] "derived called"
> [1] "base called"
>
>
> Thanx in advance,
> Simon Knapp
>
> [[alternative HTML version deleted]]
>
--
Charles C. Berry Dept of Family/Preventive Medicine
cberry at ucsd edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list