[Rd] setMethod("Logic", ...)
Martin Maechler
maechler at stat.math.ethz.ch
Mon Sep 4 11:57:15 CEST 2006
>>>>> "Robin" == Robin Hankin <r.hankin at noc.soton.ac.uk>
>>>>> on Mon, 4 Sep 2006 09:41:46 +0100 writes:
Robin> Professor Ripley
Robin> thank you for this (and indeed thank you for solving my earlier
Robin> problem on octonions).
Robin> I still don't know how to force logic operations on brob objects to
Robin> give an error.
You "just" {as we will see below, your question is more interesting
than I first thought !}
define methods for all three of
'"&"', '"|"', '"!"'
e.g.
> setClass("brob", contains="numeric")
[1] "brob"
> setMethod("!", "brob", function(e1) {cat("not a brob\n"); invisible(e1)})
[1] "!"
> !new("brob")
not a brob
>
To keep things consistent, I would use helper functions {that
I'd keep hidden in the namespace}.
## Something like
logic.brob.error <- function(name) {
stop("logic operator '", name, "' not applicable to brobs")
}
logic2 <- function(e1,e2) {
logic.brob.error(.Generic)
}
setMethod("!", "brob",
function(e1) { logic.brob.error("!") })
setMethod("&", signature("brob", "ANY"), logic2) # -- okay --
setMethod("&", signature("ANY", "brob"), logic2) # ERR ----> bug in R (?)
## Error in ...
## the method for function '&' and signature e1="ANY", e2="brob" is sealed and cannot be re-defined
setMethod("|", signature("brob", "ANY"), logic2) # -- okay --
setMethod("|", signature("ANY", "brob"), logic2) # ERR ----> bug in R (?!)
new("brob", 3) & new("brob", 0) #-> proper error
new("brob") & 1 #-> proper error
TRUE & new("brob", pi) # -> TRUE -- oops!
##--------------------------------------------------------------
I think this reveals a bug in R (still present in R-devel as of
this morning):
signature("ANY", "brob") should not be sealed for method
definition but it is.
Martin Maechler
More information about the R-devel
mailing list