Trying out Ceylon's Java interop

Posted by    |       Ceylon

Great, I'm finally able to write, compile, and run Ceylon code that uses Java libraries from within Ceylon IDE:

import java.lang { System { sysprops=properties } }
import java.util { Date }

void greet() {
    value date = Date();
    print("Hello, " sysprops.getProperty("user.name", "world") 
          ", the date is " date.day "/" date.month "/" 1900+date.year ".");
}

This doesn't look like much, perhaps, but it's demonstrating some important features of the interoperability:

  1. the ability to map a Java static declaration to a toplevel declaration in Ceylon,
  2. the ability to resolve an invocation to the correct overloaded version,
  3. the equivalence between Java primitive types and java.lang.String and Ceylon types in ceylon.language, and
  4. the automatic mapping of JavaBeans properties to Ceylon attributes.

Here's a second working example:

import java.lang { System { sysprops=properties } }
import java.io { File }

void listHomeDir() {
    for (file in File(sysprops.getProperty("user.home")).listFiles()) {
        print(file.canonicalPath);
    }
}

Java interop has been a somewhat tricky problem for us because Ceylon's type system is somewhat different to Java's, and because the design of Ceylon's language module isn't really based on the Java SDK. When running on the Java VM, the language module does make use of the Java SDK as part of its internal implementation. But when running on a JavaScript VM, it can't, of course. So we have to limit our dependence upon JVM-specific stuff.

We've still got a few things to finish off here. For example, our treatment of arrays and Java Iterables is not completely finished, and some IDE features still aren't working quite right, but I think most of the hard work is already done, ready for release as part of Ceylon M2.

Good work guys!


Back to top