Type hierarchy

Chris Seaton chris.seaton at oracle.com
Wed Apr 1 13:59:55 UTC 2015

Hello Renze,

In the past when people have developed JVM languages like Jython and JRuby they have recreated the guest language’s class hierarchy in Java. So in JRuby, there is a RubyBasicObject, a RubyObject and a RubyFixnum. These Java classes have methods that correspond to the Ruby methods in the same classes.

I would not recommend doing this for a Truffle language. I think instead you want to increase the level of abstraction slightly and think more about writing a Java program that represents the guest language object model, rather than recreating it in Java.

In the Truffle implementation of Ruby, we use int and long for Fixnum, even though they logically extend Object and BasicObject, and there is no RubyFixnum class at all. We then add special cases for all operations that examine the class of an object to return that Fixnum is the class for an int or long - we abstract the inheritance and express it in Java code, rather than expressing it using the Java object model.

To take it one stage further you can use a Truffle DyanmicObject to represent your guest language objects.

Combining all of this - not using the Java class hierarchy, not writing methods in your guest language classes, and then using DyanmicObject instead, in the end you’ll find you don’t have any need for classes like RubyObject at all (we still have them in JRuby+Truffle but they’re going away soon).

Is that helpful? Or do you have more specific reasons why you want the inheritance which make this not practical?



> On 1 Apr 2015, at 14:32, Renze Torensma <renzetorensma at gmail.com> wrote:
> Hi,
> Do you have any tips about dealing with a type hierarchy that has one superclass for all types? For example a Value superclass with Boolean, String, Int, List and Date subclasses. That way I can’t use the java primitives for boolean and int so I lose out on those performance gains I think..
> Renze

More information about the graal-dev mailing list