Thursday, February 5, 2009

Whenever I start learning about a new language on the JVM, I always find myself wanting to sprinkle the new language into my Java code. I never want to be the guy asking to scrap our project and start over in Groovy or Scala. I'd rather just drop down to Groovy to parse some XML, or write a quick Unit Test for my Java code with Scala.

The problem I run into, is that adding code from another language always either involves altering the build process to compile the other language, or adding lots of boilerplate code to interpret an external script. I really don't like either solution.

I think if Sun is going to push multiple languages on the JVM, the first thing they should work on is natural integration with those language from Java.

One solution that would be interesting is similar to GWT's JSNI. In JSNI, you include your Javascript as a comment on a native method. Rather than linking to a native library, the GWT compiler digs the javascript out and includes it in the generated Javascript. Calling code can consider it to be just another Java method:

public static native void alert(String msg) /*-{

I'm not really fond of putting the code in comments. I think that part is just a work around so Google could add the feature without breaking compatibility with IDEs and tools. Something like this might be a little cleaner:

public static void alert(String msg) {


This would be a language change to Java, of course... but I think it would be worth any extra effort. It would allow developers to use the best tool for the job at a much more granular level.

The Java compiler could just shovel the contents of the method into a JSR223 eval block without much thought. In time, other compilers, such as Scala's could be aware of this syntax and could compile the code.

There are other ways to provide support for integrating languages cleanly. Here's a great presentation on C# 4 and how its support for dynamic types will help it integrate with other languages (here's an ipod version for those who don't want to mess with Silverlight). I would never expect Java to move forward this aggressively, so I won't bother go into the details. But it's a good presentation for those who like to daydream.


Post a Comment

<< Home