powerful tools, including an Eclipse-based IDE[12]
The name "Ceylon" is an oblique reference to Java, in that Java and Sri Lanka, formerly known as Ceylon, are islands known for growth and export of coffee and tea.
In August 2017, Ceylon was donated to the Eclipse Foundation. Development slowed down and finally stopped in 2020.[13] In April 2023, Eclipse Foundation declared the termination of the transition.[14]
Language features
Ceylon is heavily influenced by Java's syntax, but adds many new features.
Type system
One of the most novel aspects of Ceylon compared to Java is its type system. Ceylon foregoes Java's primitive types[15] 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:
sharedvoidintegerOrString(Integer|Stringinput){if(isIntegerinput){print("Got the integer ``input``");}else{print("Got the string '``input``'");}}
Intersection types, written A&B, are the theoretical foundation of flow-sensitive typing:
sharedvoidintegerOrString(Integer|Stringinput){Integeradded=input+6;// illegal; the + operator is not defined on Integer|Stringif(isIntegerinput){Integeradded=input+6;// legal; input is now known to be an Integerprint("Got the integer ``input``");}else{print("Got the string '``input``'");}}
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.
Null safety
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?.[16]
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.
// A top-level higher-order function using block syntax (not associated with any user-created classes)Stringprocess(Stringtext,StringtransformString(StringtoChange)){returntransformString(text);}// A top-level function calling String.reverse in expression form.Stringreverse(Stringtext)=>text.reversed;// A function reference to String.reversed but mostly equivalent to the function above.String(String)reverseFunctionReference=String.reversed;// An example where the top-level function above is provided as an argument to the higher-order function aboveStringreversed1=process("one",reverse);// An example where an anonymous function - (text) => text+text - is provided to the higher-order function above. Stringreversed2=process("one",(text)=>text+text);
Enumerated types
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.[18]
// Traditional syntax for enumerated type, in this case, limiting the instances to three objects(for this purpose: Singletons)abstractclassVehicle(sharedStringname)ofplane|train|automobile{}objectplaneextendsVehicle("plane"){}objecttrainextendsVehicle("train"){}objectautomobileextendsVehicle("automobile"){}// Compile error: type is not a subtype of any case of enumerated supertype: 'boat' inherits 'Vehicle'//object boat extends Vehicle("boat") {}// New (as of Ceylon 1.2.0) constructor-based syntaxclassVehicleofplane|train|automobile{Stringname;abstractnewnamed(StringpName){name=pName;}sharednewplaneextendsnamed("plane"){}sharednewtrainextendsnamed("train"){}sharednewautomobileextendsnamed("automobile"){}// Compile error: value constructor does not occur in of clause of non-abstract enumerated class: 'boat' is not listed in the of clause of 'Vehicle'//shared new boat extends named("boat") {}}
Type inference
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: