Re: [R] Question on callNextMethod

2012-11-06 Thread cberry
Simon Knapp  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. BerryDept of Family/Preventive Medicine
cberry at ucsd edu  UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Question on callNextMethod

2012-11-06 Thread Martin Morgan

On 11/06/2012 07:03 AM, Simon Knapp wrote:

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
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"


Fun; I think you're seeing the print command, and also the (normally invisible) 
return value from print


> d = new('Derived')
> res = test(d)
[1] "derived called"
[1] "base called"
> res
[1] "base called"

similar to

> print("x")
[1] "x"
> (print("x"))
[1] "x"
[1] "x"

Martin




and I was expecting:

[1] "derived called"
[1] "base called"


Thanx in advance,
Simon Knapp

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Question on callNextMethod

2012-11-06 Thread Simon Knapp
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
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]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.