[Rcpp-devel] Odd-seeming behaviour of [[ and $

North, Ben Ben.North at sig.com
Mon Oct 22 10:54:06 CEST 2012


Hi,

I recently came across some behaviour of `[[` and `$` which surprised
me.  I was trying to access a method of a class implemented using Rcpp,
choosing the method name at runtime, but the example below uses a fixed
method name to keep it short.

The full C++ code:

- - - - 8< - - - -

#include <Rcpp.h>

struct Constant
{
    Constant(double x) : x(x) {}
    double value() const { return x; }
    const double x;
};

RCPP_MODULE(Constant)
{
    Rcpp::class_<Constant>("Constant")
        .constructor<double>()
        .method("value", &Constant::value);
}

- - - - 8< - - - -

and the R script with (summarised) results embedded as comments:

- - - - 8< - - - -

## Versions:
## R 2.15.1
## Rcpp 0.9.14

require(methods)
require(Rcpp)

dll.info <- dyn.load("Constant")
mod <- Module("Constant", dll.info, mustStart = TRUE)
Constant <- mod$Constant

x <- new(Constant, 2.2)

x[["value"]]
## NULL

x$value
## Class method definition for method value()
## [...]

x[["value"]]
## Class method definition for method value()
## [...]

- - - - 8< - - - -

The fact that 'x[["value"]]' finds the method only if I've first done
'x$value' seemed odd.

Thanks,

Ben.


________________________________

Susquehanna International Group Limited

Susquehanna International Group Limited is a private company limited by shares and registered in Ireland. Registration No.: 445356. Registered Address: 4th Floor, Georges Dock House, IFSC, Dublin 1, Ireland.
Susquehanna Ireland Limited is a private company limited by shares and registered in Ireland. Registration No.: 305632. Registered Address: 4th Floor, Georges Dock House, IFSC, Dublin 1, Ireland.
Susquehanna International Securities Limited is a private company limited by shares and registered in Ireland. Registration No.: 337946. Registered Address: 4th Floor, Georges Dock House, IFSC, Dublin 1, Ireland.

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


More information about the Rcpp-devel mailing list