[Rd] setIs and method dispatch in S4 classes
Seth Falcon
sfalcon at fhcrc.org
Fri Apr 7 16:56:56 CEST 2006
Hi Peter,
Peter Ruckdeschel <Peter.Ruckdeschel at uni-bayreuth.de> writes:
> ## now: B00 mother class to B01 and B02, and again B02 "contains" B01 by
> setIs:
> setClass("B00", representation(a="numeric"))
> setClass("B01", representation(a="numeric",b="numeric"), contains= "B00")
> setClass("B02", representation(a="numeric",d="numeric"), contains= "B00")
> setIs("B02","B01",coerce=function(obj){new("B01", a=obj at a, b=obj at d)},
> replace=function(obj,value){new("B01", a=value at a, b=value at b)})
>
> # now two "+" methods for B00 and B01
> setMethod("+", signature=c("B00","B00"), function(e1,e2){e1 at a+e2@a})
> setMethod("+", signature=c("B01","B01"), function(e1,e2){e1 at b+e2@b})
>
> x1=new("B02", a=1, d=2)
> x2=new("B02", a=1, d=3)
>
> x1+x2 ## 2 --- why?
My impression from reading over the man page for setIs, is that it
isn't intended to be used to override the existing inheritance
hierarchy. It also mentions that the return value is the extension
info as a list, so that could also be useful in understanding what
setIs is doing. Here's the output for your example:
Slots:
Name: a d
Class: numeric numeric
Extends:
Class "B00", directly
Class "B01", directly, with explicit coerce
Use the contains arg of setClass to define the superclasses. With the
contains arg, the order determines the precedence for method lookup.
But I suspect you know that already.
> Is there a possibility to force usage of the B01 method /without/
> explicitely coercing x1,x2 to B01, i.e. interfere in the dispatching
> precedence, telling R somehow (by particular arguments for setIs ?)
> to always use the is-relation defined by setIs first before mounting
> the hierarchy tree?
Perhaps explaining a bit more about what you are trying to accomplish
will allow someone to provide a more helpful suggestion than mine :-)
If you know the inheritance structure you want before run-time, then
I'm not seeing why you wouldn't just use the contains arg.
And if you want to force certain behavior at run-time, then I don't
see what's wrong with an explicit coercion using as(foo, "bar").
But perhaps I'm missing something.
Cheers,
+ seth
More information about the R-devel
mailing list