Help

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!

5 comments:
 
27. Feb 2012, 00:09 CET | Link

Well done! Do you plan realizing Java interop and Union/Intersection Type compatibility?

Like: import java.util { Date } DateCeylonClass = Date(); And how about Nullsafety? Are all used Java classes implizit Nothing Union Types or do you assure that references of Java variables can not be null?

Best regards, Frank

ReplyQuote
 
27. Feb 2012, 00:11 CET | Link
My comment broke because of the Ceylon example. ;-)
Correct version:

Well done!
Do you plan realizing Java interop and Union/Intersection Type compatibility?

Like:
import java.util { Date }
Date|CeylonClass = Date();

And how about Nullsafety? Are all used Java classes implizit |Nothing Union Types or do you assure that references of Java variables can not be null?

Best regards,
Frank


Click HELP for text formatting instructions. Then edit this text and check the preview.
 
27. Feb 2012, 18:38 CET | Link
Like:
import java.util { Date } 

Date|CeylonClass obj = Date();

That should work just fine. :-)

And how about Nullsafety? Are all used Java classes implizit |Nothing Union Types or do you assure that references of Java variables can not be null?

Our approach is to let the programmer decide. You can write:

Date date = methodThatYouKnowNeverReturnsNull();

And, if you're wrong, and it actually does return null, you'll get an NPE at this line.

Or you can write:

Date? date = methodThatYouThinkSometimesReturnsNull();

Unfortunately, Java's typesystem doesn't capture information about which methods can return null, so this is the best we can do. But I think it's a very reasonable approach in light of that. I would not want to go and make you have to write if (exists) every time you call a Java method. Nor am I going to go and try and compiler a catalog of all methods in all Java libraries that can't return null. That to me just sounds nuts and extremely error-prone.

 
29. Feb 2012, 15:55 CET | Link

I think that java needs a CLS common lenaguage specification that the microsoft plaftform do for interoperability between languages.

All the people is making a new language but the other languages maybe can not interop with others because Java doesn't have a CLS.

 
29. Feb 2012, 23:14 CET | Link
I think that java needs a CLS common lenaguage specification that the microsoft plaftform do for interoperability between languages.

Well, it sort-of does in the sense that this is defined by the bytecodes and class file format. But yeah, I agree that the level of abstraction is a not quite right, and that interop is a bit limited to the language subsets which map directly to Java.

I agree that it would be wonderful if the JCP could take on the role of trying to improve interop between JVM languages. That seems to be something where the JCP could really show its value.

Post Comment