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

Romain Francois romain at r-enthusiasts.com
Mon Oct 22 11:08:36 CEST 2012


Hello,

This is an implementation artefact. In short, you are not supposed to 
use [[.

Romain

Le 22/10/12 10:54, North, Ben a écrit :
> 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.



-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
R Graph Gallery: http://gallery.r-enthusiasts.com
blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/xbKv0R : Crawling facebook with R
|- http://bit.ly/v3WB8S : ... And now for solution 17, still using Rcpp
`- http://bit.ly/uaQDGr : int64: 64 bit integer vectors for R



More information about the Rcpp-devel mailing list