Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#1221 overload dispatch in R is broken for "$" operator

closed
Joseph Wang
r (7)
5
2012-04-15
2012-01-24
Robert Day
No

For a simple class like Base (in the attached Base.h), the following R calls are fine:

> b <- Base()
> Base_method(b)
[1] 42
> Base_overloaded_method(b)
[1] 42
> Base_overloaded_method(b, 43)
NULL
> Base_overloaded_method(b)
[1] 43
> b$method()
[1] 43

But these are broken:

> b$overloaded_method()
Error in mapply(class, list(...)) : '...' used in an incorrect context
3: b$overloaded_method()
2: mapply(class, list(...))
1: function ()
{
traceback(tail(sapply(sys.calls(), deparse), 100))
if (!interactive())
q(save = "no", status = 1)
}()
> b$overloaded_method(42)
Error in b$overloaded_method(42) : could not find function "f"
2: b$overloaded_method(42)
1: function ()
{
traceback(tail(sapply(sys.calls(), deparse), 100))
if (!interactive())
q(save = "no", status = 1)
}()

> R --version
R version 2.14.0 (2011-10-31)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i686-pc-linux-gnu (32-bit)

> swig -version
SWIG Version 2.0.4
Compiled with g++ [i686-pc-linux-gnu]
Configured options: +pcre

Discussion

  • Robert Day
    Robert Day
    2012-01-24

    Base header file

     
    Attachments
  • Robert Day
    Robert Day
    2012-01-24

    • summary: overload dispatch is broken for "$" operator --> overload dispatch in R is broken for "$" operator
     
  • Robert Day
    Robert Day
    2012-01-27

    A solution for the first error:

    > b$overloaded_method()
    Error in mapply(class, list(...)) : '...' used in an incorrect context

    is to change the signature of the overload dispatch function from:
    `Base_overloaded_method` <- function(...)
    to:
    `Base_overloaded_method` <- function(self, ...)
    and make a few corresponding changes in the body of the function.

    The second error is due to the way the expression "class$method(args)" works. `$` is a binary operator on 'class' and 'method'. It returns a function object, say 'f', so that the expression becomes "f(args)". In the `$` function, the class object, say 'x', is bound to the first argument of 'f' with the line:
    formals(f)[[1]] = x;
    But apparently calling "f(args)" overrides the bound x value, so 'f' doesn't have a reference to the class object. Might it be possible to store the x object in an environment for 'f'? I am new to R, but I will try this.

     
  • Joseph Wang
    Joseph Wang
    2012-04-15

    • status: open --> open-fixed
     
  • Joseph Wang
    Joseph Wang
    2012-04-15

    This bug has been fixed with the latest round of R changes. I have checked in a regression test that confirms the fix

    > b <- Base()
    > Base_method(b)
    [1] 42
    > Base_overloaded_method(b)
    [1] 42
    > Base_overloaded_method(b, 43)
    NULL
    > Base_overloaded_method(b)
    [1] 43
    > b$method()
    [1] 43
    >
    > b$overloaded_method()
    [1] 43

     
  • William Fulton
    William Fulton
    2012-04-15

    • status: open-fixed --> closed
     
  • William Fulton
    William Fulton
    2012-04-15

    Closing