From Waclaw.Marcin.Kusnierczyk at idi.ntnu.no  Mon Feb 23 21:05:26 2009
From: Waclaw.Marcin.Kusnierczyk at idi.ntnu.no (Wacek Kusnierczyk)
Date: Mon, 23 Feb 2009 21:05:26 +0100
Subject: [Rd] a question about (
Message-ID: <49A30186.7040000@idi.ntnu.no>

i wonder why the following approach to make an 'object' executable could
not be made to work:

    foo = 1:3
    class(object) = c('foo', class(foo))
    '(.foo' = function(foo, fun) sapply(foo, fun)

    foo
    # 1 2 3
   
    foo(function(x) x^2)
    # error: no function foo defined

the actual example is inessential, and is inspired by the __call__
method available in python.

what happens is, as of my understanding, that r sees an application
expression with the operator 'foo', and so tries to find a so-named
function, and there is none, hence the error.  however, there *is* an
object named 'foo' there, and so it would, in principle, be possible to
make r try in turn dispatching '(' on the object's type.  this would
lead to the function (.foo, and a successful application.

    isGeneric('(')
    # error: methods may not be defined for primitive function "(" in
this version of R

makes me wonder whether making ( a generic function that would work
along the lines sketched above was actually considered for future
versions of r.  (or has it actually been removed?)

vQ



[Rd] a question about (
Wacek Kusnierczyk