[graalvm-users] How to remove/opearte on an R vector through Interop?

Stepan stepan.sindelar at oracle.com
Sat Jun 16 14:25:39 PDT 2018


Hi Rodrigo,

I think the idea of Truffle interop is that once the object, in this 
case, R vector, goes into another language, the other language tries to 
treat it as if it were its "native" object. In this case, it means Ruby 
tries to apply the semantics of Ruby indexing to R vector. R vectors are 
immutable and cannot be modified, what looks like you're modifying data 
of a vector in R:

myVector[42] <- newValue

is in fact translated into (btw. you can execute this snippet as is too):

myVector <- `[<-`(myVector, 42, newValue)

There's a function called `[<-` that takes the vector as a first 
parameter and returns a new vector. The old vector stays unchanged (or 
at least any modifications must not be observable).

What you can do is to retrieve the `[<-` function from R and use it:

Ruby code:
vec = Polyglot.eval("R", "c(1,2,3)")
subsetAssign = Polyglot.eval("R", "`[<-`")
vec = subsetAssign.call(vec, 1, 42)
p vec[0]

I don't know much of Ruby, but if Ruby allows overriding operators like 
"[" you can create a Ruby class that wraps R vector and delegates all 
the Ruby operations to appropriate R operations. This is what Rcpp 
package does with R and C++. Rcpp uses the ability to override various 
operators in C++ to provide R-like embedded DSL in C++ using the R to C 
interface (btw. supported by both GNU R and R in GraalVM). Building 
something like this in Ruby or any such flexible language would be an 
interesting project.

We could, of course, support this approach natively in the Truffle 
interop, but that would mean doing the mapping of one semantics to 
another for all the language pairs, and interop aims to be generic. I 
think the current interop approach provides all the necessary primitives 
to implement such mapping in user code.

Regards,
Stepan



More information about the GraalVM-Users mailing list