Ceylon is heavily influenced by Java's syntax, but adds many new features.
One of the most novel aspects of Ceylon compared to Java is its type system. Ceylon foregoes Java's primitive types10 and boxing in favor of a type system composed entirely of first-class objects. While this may cause boxing overhead in some situations, it makes the type system more uniform.
Ceylon allows for union and intersection types, in a similar fashion to TypeScript, Whiley and Flow, which in fact, took the idea from Ceylon.
Union types, written A|B, allow a variable to have more than one type. The following example shows a Ceylon function which may take either an integer or a string:
Intersection types, written A&B, are the theoretical foundation of flow-sensitive typing:
The condition is Integer input narrows the type of input to <Integer|String> & Integer, which distributes to Integer&Integer | String&Integer, which, as String and Integer are disjoint types, is equivalent to Integer&Integer | Nothing (Nothing is the empty bottom type), which simplifies to just Integer.
Union and intersection types are used to provide null safety. The top type of the Ceylon type hierarchy is the class Anything, which has two subclasses: Object, the superclass of all normal classes and all interfaces, and Null, with the only instance null. Since Object and Null are disjoint types, most regular types like Integer or List<String> are not nullable; a nullable type is the union Integer|Null, abbreviated Integer?.11
Intersection types can be used to get a non-optional type out of a possibly-optional type, such as a type parameter. For example, the signature of a function that removes null elements from a stream of values could be:
When removeNulls is called with a stream of Integer|Null elements, the result will be a stream of <Integer|Null> & Object elements, which simplifies to Integer.
Similarly to many modern languages, Ceylon supports first class functions and higher order functions, including function types and anonymous functions12
Similar to Java and many other languages, and with a similar mechanism as algebraic types, Ceylon supports enumerated types, otherwise known as enums. This is implemented in Ceylon with a pattern of limiting the instances of an abstract class at declaration to a limited set of objects (in this case, singleton instances). Another way to implement this pattern is with the new constructor feature in Ceylon 1.2 where the objects are implemented as different named constructor declarations.13
Ceylon is strongly and statically typed, but also has support for type inference. The value keyword is used to infer the type of a variable, and the function keyword is used to infer the type of a function. The following two definition pairs are each equivalent:
However, to make single-pass type inference possible, type inference is only allowed for non-toplevel and unshared declarations.14
By default the starter (ceylon run) runs the shared run() function of a module:
but any other shared function without parameters can be used as main calling the program with the run parameter, like this:
ceylon run --compile=force --run hello default
Versions of Ceylon released:15
All parts of Ceylon are available as free software, mostly the Apache License.16 Part of the source code is licensed under LGPL.
"Ceylon 1.0 beta". Retrieved 2013-09-26. http://ceylon-lang.org/blog/2013/09/22/ceylon-1/ ↩
"Project Ceylon – Red Hat builds Java replacement". The Register. 2011-04-13. Retrieved 2011-11-27. https://www.theregister.co.uk/2011/04/13/red_hat_unveils_project_ceylon ↩
King, Gavin (2012-01-10). "Principles that guide this project". Retrieved 2015-12-04. http://ceylon-lang.org/blog/2012/01/10/goals/ ↩
"FAQ about language design: Goals". Retrieved 2015-12-04. http://ceylon-lang.org/documentation/1.2/faq/language-design/#goals ↩
Festal, David (2014-10-10). "Write in Ceylon, deploy as OSGI, use in Java EE". Retrieved 2015-12-04. http://ceylon-lang.org/blog/2014/10/10/ceylon-osgi-jee/ ↩
"Maven repositories". Retrieved 2015-12-04. http://ceylon-lang.org/documentation/1.2/reference/repository/maven/#maven_repositories ↩
"Features of Ceylon IDE". Retrieved 2015-12-04. http://ceylon-lang.org/documentation/1.2/ide/features/ ↩
"ceylon / ceylon". GitHub, Inc. 2020-05-25. Archived from the original on 2023-10-03. Retrieved 2024-01-22. https://github.com/eclipse-archived/ceylon ↩
"Eclipse Ceylon™ Termination Review". Eclipse Foundation. 2023-04-05. Archived from the original on 2023-04-23. Retrieved 2023-04-23. https://projects.eclipse.org/projects/technology.ceylon/reviews/termination-review ↩
King, Gavin. "Ceylon: Language Design FAQ". http://ceylon-lang.org/documentation/faq/language-design/ ↩
King, Gavin. "The Ceylon Language: §1.4.3 Compile-time safety for null values and flow-sensitive typing". Retrieved 2015-12-04. http://ceylon-lang.org/documentation/1.2/spec/html_single/#compiletimesafety ↩
King, Gavin. "The Ceylon Language: 4.7 Functions". Retrieved 5 December 2015. http://ceylon-lang.org/documentation/1.2/spec/html/declarations.html#functions ↩
King, Gavin. "The Ceylon Language: 4.5.8 Enumerated classes". Retrieved 6 December 2015. http://ceylon-lang.org/documentation/1.2/spec/html/declarations.html#classeswithcases ↩
King, Gavin. "The Ceylon Language: §3.2.9 Type inference". Retrieved 2015-12-04. http://ceylon-lang.org/documentation/1.2/spec/html_single/#typeinference ↩
https://ceylon-lang.org/download-archive/ Ceylon: Download Previous Ceylon versions https://ceylon-lang.org/download-archive/ ↩
"Ceylon: Licenses". Retrieved 2015-12-04. http://ceylon-lang.org/code/licenses ↩