Re: [Rd] setMethod("Logic", ...)

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Mon 04 Sep 2006 - 09:57:15 GMT

>>>>> "Robin" == Robin Hankin <r.hankin@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



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon Sep 04 20:00:11 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 06 Sep 2006 - 11:39:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.