A first look at Gavin King's Ceylon

Gavin King brought us Hibernate and Seam. Now he wants us to drink the Kool-Aid -- or rather the Ceylon tea -- once more

Page 2 of 4

Importing
Notice that Ceylon imports a bit differently than its Java parent. There is the cosmetic issue of the bracket notation, but it goes deeper. I can import not merely classes or packages, but also functions and values. Unlike with Java, this applies not just to static methods. In fact, Ceylon does not have static methods, but any method can be imported.

In the sample above, I imported the getConnection method from java.sql.DriverManager. I can now call the getConnection method directly from my own method:

Connection getDbConnection() {

String userName = "granny";

   String password = "granny";

   String url = "jdbc:postgresql://localhost/grannydb";

   forName("org.postgresql.Driver");

   return getConnection (url, userName, password);

}

Naming conflicts between packages are a frustrating problem in Java. One of the most common in the early days of Java was java.util.Date and java.sql.Date. In the case of importing both in the same class, you always had to refer to them by their fully qualified name. Ceylon imports allow you to use aliases instead.

import java.sql { SqlDate=Date }

import java.util { Date }

Java language integration
You may notice that I imported some Java packages above. Ceylon doesn't rebuild the Java universe (at the moment). I'm using Java's JDBC support directly. For the purpose of demonstrating Ceylon, I use the old-school Java DriverManager and JDBC driver. I could have wired up a connection pool, but let's face it -- Granny won't need scalability for her address book.

When I use Java from Ceylon, I get most of the features of Ceylon classes and functions even if they are really Java classes and methods. I can import individual methods, alias items, and more. One caveat: This sometimes means little conflicts with the type system. In particular, a lot of Java stuff returns collections that appear to be the same as the Ceylon collections, but are not. I accidentally used a Java ArrayList and wondered why I couldn't run it in a Ceylon for/each statement. For Ceylon to iterate over it, it needed to support one of Ceylon's interfaces.

It may sound contradictory, but while Java integration is one of Ceylon's best features, it's where I messed up the most. When typing familiar Java APIs in a different syntax, when the compiler says no, I have trouble "thinking" in Ceylon when typing Java APIs.

JavaScript integration
In the interest of time, I reused an existing JavaScript front end from the "7 programming languages in 7 days" article. In theory, I could have written the front end in Ceylon. This is the best argument against the idea that the future might be written entirely in JavaScript -- and in support of the idea that JavaScript is the assembly language of the future. You can write Ceylon and target Node.js, the browser, or possibly any other JavaScript environment.

| 1 2 3 4 Page 2
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies