[Rd] setMethod() woes
Robin Hankin
r.hankin at noc.soton.ac.uk
Fri Sep 15 15:56:56 CEST 2006
Hello everybody
R version 2.4.0 alpha (2006-09-15 r39323), MacOSX 10.4.7
Next S4 problem.
I have "brob" objects that are large real numbers, and now I want "glub"
numbers that are to be a pair of glubs that represent complex numbers.
I want to define binary operator "+" so that if either the left or right
argument are glubs, it uses .ArithGlub.
If either argument is a brob (but neither is a glub), use .ArithGlub
If neither is a brob or a glub, use standard "+"
(just like real/complex "+").
My attempt (self contained, minimal) is included below.
But, with this, the following session shows that something is wrong:
> a <- new("brob",x=1:5,positive=rep(T,5))
> b <- new("glub",real=a,imag=a)
> a+b
Error in a + b : binary operator " + " not defined for Brobdingnagian
numbers
In addition: Warning message:
Ambiguous method selection for "+", target "brob#glub" (the first of
the signatures shown will be used)
brob#ANY
ANY#glub
in: .findInheritedMethods(classes, fdef, mtable)
I don't understand what to do to avoid getting the warning
message [and it's also using the wrong function: I want it
to call .ArithGlub() here, not .ArithBrob() ]
I need to use signature(e1="brob", e2="anything except a glub")
but I can't figure out how to do this.
setClass("brob",
representation = representation
(x="numeric",positive="logical"),
prototype = list(x=numeric(),positive=logical())
)
setClass("glub",
representation = representation(real="brob",imag="brob"),
prototype = list(real=new("brob"), imag=new("brob"))
)
.ArithBrob <- function(e1,e2){
stop(paste("binary operator \"", .Generic, "\" not defined for
Brobdingnagian numbers"))
}
.ArithGlub <- function(e1,e2){
stop(paste("binary operator \"", .Generic, "\" not defined for
Glub numbers"))
}
setMethod("Arith",signature(e1 = "brob", e2="brob"), .ArithBrob)
setMethod("Arith",signature(e1 = "brob", e2="ANY" ), .ArithBrob)
setMethod("Arith",signature(e1 = "ANY" , e2="brob"), .ArithBrob)
setMethod("Arith",signature(e1 = "glub"), .ArithGlub)
setMethod("Arith",signature(e2 = "glub"), .ArithGlub)
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
More information about the R-devel
mailing list