JRuby’s main compiler is complete, so I’m starting to kick around ideas
for a second compiler.
The goal of the second compiler would be to produce normal-looking Java
classes that are actually just Ruby code underneath. Constructing them
in the normal Java way would invoke initialize; calling methods in the
normal Java way would dynamically call the associated method.
I talk about it in the second half of this post:
Now before anyone cries foul, this is not about adding static typing to
Ruby; it’s about finding a way to specify what types should be inserted
into the generated Java class to make things easy on the Java side,
while avoiding incompatible changes to JRuby.
Since so many of you have a better perception of beauty in DSL-like
syntaxes than I do, I figured you might have a good suggestion.
The first version I proposed on the blog would be a simple map in the
class body of the form
{ => [<arg type 1>, <arg type 2> …]}
as in
{String => [Integer, Array]}
def mymethod(i, a) …
which would appear to Java code as
public String mymethod(Integer i, List a) { …
or perhaps
public String mymethod(int i, List a) { …
But I know it’s not particularly pretty. So here’s the requirements:
- provide a way to specify the types of an immediately-following method
definition; this is only going to be a compiler hint, so it doesn’t have
to (and perhaps should avoid) having any side-effects. - have no lasting side-effects when run nomally. The above code would
just produce a hash that gets immediately thrown away if run in e.g.
Matz’s Ruby. - not require any syntactic or library changes to Ruby, so the same code
can run unmodified on any implementation.
Ideas?
- Charlie