; last updated - 3 minutes read

Maybe you've already heard of Ceylon. Ceylon's a language initiated by Gavin King, the creator of both Hibernate and Seam. Putting it in a nutshell, Ceylon is an attempt to create a better version of Java.

The Ceylon developer team has released an early preview version (milestone 5). Andrew C. Oliver considered it to be mature enough to have a first look at it (see his article at Infoworld.com).

A very nice feature of Ceylon is the approach to NullPointerExceptions. This kind of mistake is a real plague of the Java world. Usually they are caused by programmers who forgot to implement the "nasty case". This is why I baptized the NullPointerException a NotImplementedYetException in disguise in an earlier article.

Ceylon solves the problem by adding an option type to the language. In a way, this is similar to Java 8's and Guava's Optional class. The optional type can hold a value, or it can be empty. However, it can't be null.

The nice thing about Ceylon's option type is it's implemented directly in the core of the language. In general, values can never be null. If you need empty values, you define the variable slightly differently. Instead of using the type of the variable, you use the corresponding option type by adding a question mark:

Address? address= getAdress("John Doe"); if (exists address) { print address.city; } else { print "No address found."; } println address.zipCode; // Compiler error

It's the compiler's job to verify if you've thought of the nasty use cases as well as the desired use cases. Of course, the language extension enables the IDE to help you as well. This goes way beyond what Eclipse already does today. Eclipse recognizes a lot of potential NullPointerExceptions, but if can't do so across method calls - unless you give it a hint using Eclipse Juno's proprietary @NonNull, @NonNullByDefault and @Nullable annotations:

Actually, the annotation is almost the same approach as Ceylon's option types. But it's only an IDE extension, and it's easy to forget to annotate a variable. Being one of the core features of the language, Ceylon's approach prevents this completely, giving Ceylon an edge over Java.

Well - almost completely. There's a catch, and it's called Java. Like every other JVM language Ceylon code can call Java methods, and inevitably this is a point of friction. Java methods generally accept and return null values without mentioning it. So Ceylon has to treat every Java return value as an option type. Doing so leads to a ridiculous number of null checks (just what the average Java code looks like :)). The Ceylon developers decided to compromise in order to maintain your code clean. The drawback of the compromise is that calling Java code can result in a NullPointerException. If you're interested in the details, read Gavin King's explanation.


Short introduction to Ceylon on the Ceylon project page

Dealing with null values in Java functions called by Ceylon code

"We should love null", Christian Neumanns claims in his comprehensive article about NullPointers and different approaches to deal with them (including Ceylon's approach).

Lifted operators in C#

Discussion on Reddit about this article


Comments