Ceylon is an object-oriented, strongly statically typed programming language with an emphasis on immutability, created by Red Hat. It is based on the Java programming language. Ceylon programs run on the Java Virtual Machine, and can be compiled to JavaScript.[6][7]
Ceylon | |
---|---|
![]() | |
Paradigm | Object-oriented |
Designed by | Gavin King, Red Hat |
First appeared | 2011 |
Stable release | 1.2.0[1]
/ October 29, 2015 |
Typing discipline | Static, strong, safe |
License | Apache v2 |
Filename extensions | .ceylon[2] |
Website | ceylon-lang |
Influenced by | |
Java[3], Scala, Smalltalk, ML[4], Lisp[5] |
Ceylon aims at solving the following problems its developers experienced with Java:[8][9][10]
- improved structured data and user interfaces
- language level modularity
- support for first-class and higher-order functions
- do away with clumsy metaprogramming
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.
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 is its type system. Ceylon foregoes java's primitive types[11] and boxing in favor of a type system composed entirely of first-class objects.
More uniquely, Ceylon allows for union and intersection types.
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:
shared void integerOrString(Integer|String input) {
if (is Integer input) {
print("Got the integer ``input``");
} else {
print("Got the string '``input``'");
}
}
Intersection types, written A&B
, are the theoretical foundation of flow-based typing:
shared void integerOrString(Integer|String input) {
Integer added = input + 6; // illegal; the + operator is not defined on Integer|String
if (is Integer input) {
Integer added = input + 6; // legal; input is now known to be an Integer
print("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?
.[12]
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:
Iterable<Element&Object> removeNulls<Element>(Iterable<Element> stream);
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
.
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:
Integer i = 3;
value i = 3;
Integer add(Integer i1, Integer i2) {
return i1 + i2;
}
function add(Integer i1, Integer i2) {
return i1 + i2;
}
However, to make single-pass type inference possible, type inference is only allowed for non-toplevel and unshared declarations.[13]
License
All parts of Ceylon are available under open source licenses, mostly the Apache License.[14]
See also
- Dart (programming language), has its own VM, compiles to JS. Type system not very strict, supports mixins.
- Fantom (programming language), compiles to JVM. Type system not very strict, supports mixins.
References
- ^ King, Gavin. "Ceylon 1.2.0 is now available". Retrieved 29 October 2015.
- ^ King, Gavin. "The Ceylon Language: §4.1 Compilation unit structure". Retrieved 2015-12-04.
A compilation unit is a text file, with the filename extension
.ceylon
. - ^ "Frequently Asked Questions: What is Ceylon?". Retrieved 2015-12-04.
Ceylon is a new programming language that's deeply influenced by Java
- ^ "ceylon/user - Gitter". Retrieved 2015-12-04.
- ^ "ceylon/user - Gitter". Retrieved 2015-12-04.
- ^ "Ceylon 1.0 beta". Retrieved 2013-09-26.
- ^ "Project Ceylon – Red Hat builds Java replacement". The Register. 2011-04-13. Retrieved 2011-11-27.
- ^ Introducing the Ceylon Project – Gavin King presentations at QCon Beijing 2011
- ^ Gavin King (2011-04-13). "Ceylon". Retrieved 2011-11-27.
- ^ "Ceylon JVM Language". infoq.com. 2011-04-13. Retrieved 2011-11-27.
First, I never billed this as a Java Killer or the next generation of the Java language. Not my words. Ceylon isn't Java, it's a new language that's deeply influenced by Java, designed by people who are unapologetic fans of Java. Java's not dying anytime soon, so nothing's killing it
- ^ King, Gavin. "Ceylon: Language Design FAQ".
- ^ King, Gavin. "The Ceylon Language: §1.4.3 Compile-time safety for null values and flow-sensitive typing". Retrieved 2015-12-04.
- ^ King, Gavin. "The Ceylon Language: §3.2.9 Type inference". Retrieved 2015-12-04.
- ^ "Ceylon: Licenses". Retrieved 2015-12-04.