[Rd] a question about (

From: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk_at_idi.ntnu.no>
Date: Mon, 23 Feb 2009 21:05:26 +0100


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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 23 Feb 2009 - 19:13:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 24 Feb 2009 - 07:30:44 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.

list of date sections of archive