[graalvm-users] How to dynamically check type

Chris Seaton chris.seaton at oracle.com
Tue Jun 12 13:49:06 PDT 2018


Hello Rodrigo,

We’ve done a huge amount of back and forth on how interop should work. From experience, what seems like the obvious solution for the code in front you at the time, then does not make any sense for the next code you want to work.

Things like adding standard Ruby Object methods like `is_a?` to foreign object gets very messy very quickly. Would they take priority over foreign methods or not? Maybe it makes sense for a small set of methods? We don’t tend to think it’s something that will work if we just add things like this ad-hoc, but we don’t have a better overall solution either.

I could perhaps be convinced about `is_a?` to be honest, though. We have added `to_s` for example.

It’s an open research problem, and we’re interested in what people who are actually trying to use interop, such as yourself, have to say.

In your case you can just massage it a bit until it works, anyway, by writing instead:

p Integer === val

Chris

> On 12 Jun 2018, at 21:39, Rodrigo Botafogo <rodrigo.a.botafogo at gmail.com> wrote:
> 
> Hello,
> 
> Is there a way to dynamically check the type of a Truffle::Interop object?
> 
> For instance, in this code:
> 
> # evaluate an integer in R and return the result of this evaluation
> # this is just an integer
> var = R.eval("4")
> puts ("Double evaluated in R: #{var}")
> p var.is_a? Integer
> 
> >> false
> 
> # Create a list in R, that is anonymous in R but available to Ruby.
> # We are now using method_missing to resolve the 'c' method.
> val = R.c(1, 2, 3, 4)
> p val
> 
> >> #<Truffle::Interop::Foreign at 299cab08>
> 
> p val.is_a? Integer
> 
> will raise an exception on the val.is_a? Integer 
> 
> Internal error occured: org.graalvm.polyglot.PolyglotException: org.truffleruby.language.control.RaiseException: Message not supported: INVOKE (UnsupportedMessageException)
> 
> I understand the message and why, but this kind of breaks the ability to check types dynamically and every code that does that.  
> 
> As a programmer, I would like to see Interop inherit from the Object Class so that it works as any other Ruby object.  Better yet, if I could identify at runtime the language the Interop pointer is referencing: Ruby, Python, etc.  Is this possible?
> 
> Probably I'm missing something... comments?
> 
> 
> Thanks a lot
> 
> -- 
> Rodrigo Botafogo
> _______________________________________________
> GraalVM-Users mailing list
> GraalVM-Users at oss.oracle.com
> https://oss.oracle.com/mailman/listinfo/graalvm-users




More information about the GraalVM-Users mailing list