From lawrence.michael at gene.com  Fri Sep 22 19:04:33 2017
From: lawrence.michael at gene.com (Michael Lawrence)
Date: Fri, 22 Sep 2017 10:04:33 -0700
Subject: [Rd] S4 method implementation for S3 class
In-Reply-To: <CALEXWq2Pfutuh1Na5cUaopUyup7WYPddd2HYKBhYGdxesabm0A@mail.gmail.com>
References: <CALEXWq2Pfutuh1Na5cUaopUyup7WYPddd2HYKBhYGdxesabm0A@mail.gmail.com>
Message-ID: <CAOQ5NycG-TAu-QAj74Z_LZOurNTPB-yRyGuCpOTkBpGfytvFoQ@mail.gmail.com>

The %*% function is a primitive. As it says in the documentation under
?Methods_Details

     Methods may be defined for most primitives, and corresponding
     metadata objects will be created to store them. Calls to the
     primitive still go directly to the C code, which will sometimes
     check for applicable methods. The definition of ?sometimes? is
     that methods must have been detected for the function in some
     package loaded in the session and ?isS4(x)? is ?TRUE? for the
     first argument (or for the second argument, in the case of binary
     operators).

But:
> isS4(x)
[1] FALSE

I think this behavior is in the interest of performance. It avoids
adding S4 dispatch overhead to e.g. matrix objects.

In general, it's best to define an S4 class when using S4 dispatch,
but it sounds like you're stuck using some legacy S3 objects. In that
case, one would normally define an S3 method for `%*%()` that
delegates to a custom non-primitive generic, perhaps "matmult" in this
case. But since %*% is not an S3 generic, that's not an option.

It would help to hear more about the context of the problem.

Michael



On Fri, Sep 22, 2017 at 5:44 AM, I?aki ?car <i.ucar86 at gmail.com> wrote:
> Hi all,
>
> I'm trying to implement the matrix multiplication operator, which is
> S4 generic, for an old-style S3 class. The following works as
> expected:
>
> x <- 1:10
> class(x) <- "myClass"
>
> setOldClass("myClass")
> setGeneric("myMethod", function(x, y) standardGeneric("myMethod"))
> setMethod("myMethod", c("myClass", "myClass"), function(x, y)
> message("dispatched!"))
>
> myMethod(x, x)
> #> dispatched!
>
> but I don't understand why the following won't:
>
> setMethod("%*%", c("myClass", "myClass"), function(x, y) message("dispatched!"))
>
> x %*% x
> #>      [,1]
> #> [1,]  385
>
> Is this approach wrong?
>
> Regards,
> I?aki
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] S4 method implementation for S3 class
Michael Lawrence