Ceylon (programming language)

This is an old revision of this page, as edited by Galaktos (talk | contribs) at 16:15, 4 December 2015 (Type System: improve and expand). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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.[5][6]

Ceylon
ParadigmObject-oriented
Designed byGavin King, Red Hat
First appeared2011
Stable release
1.2.0[1] / October 29, 2015; 9 years ago (2015-10-29)
Typing disciplineStatic, strong, safe
LicenseApache v2
Filename extensions.ceylon[2]
Websiteceylon-lang.org
Influenced by
Java, Scala, Smalltalk, ML[3], Lisp[4]

Ceylon aims at solving the following problems its developers experienced with Java:[7][8][9]

  1. improved structured data and user interfaces
  2. language level modularity
  3. support for first-class and higher-order functions
  4. 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[10] 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?.[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:

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.[12]

License

All the work, including the website, the language specification, and Ceylon Herd, is freely available under open source licenses.[13]

See also

References

  1. ^ King, Gavin. "Ceylon 1.2.0 is now available". Retrieved 29 October 2015.
  2. ^ 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.
  3. ^ "ceylon/user - Gitter". Retrieved 2015-12-04.
  4. ^ "ceylon/user - Gitter". Retrieved 2015-12-04.
  5. ^ "Ceylon 1.0 beta". Retrieved 2013-09-26.
  6. ^ "Project Ceylon – Red Hat builds Java replacement". The Register. 2011-04-13. Retrieved 2011-11-27.
  7. ^ Introducing the Ceylon Project – Gavin King presentations at QCon Beijing 2011
  8. ^ Gavin King (2011-04-13). "Ceylon". Retrieved 2011-11-27.
  9. ^ "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
  10. ^ King, Gavin. "Ceylon: Language Design FAQ".
  11. ^ King, Gavin. "The Ceylon Language: §1.4.3 Compile-time safety for null values and flow-sensitive typing". Retrieved 2015-12-04.
  12. ^ King, Gavin. "The Ceylon Language: §3.2.9 Type inference". Retrieved 2015-12-04.
  13. ^ licences, official website