Jump to content

Ceylon (programming language)

From Alfawiki
Adapted from the English Wikipedia article Ceylon (programming language), available under CC BY-SA 4.0Updated 9 August 2025
Ceylon
Company typeProgramming language
IndustrySoftware development
Founded2011; 15 years ago (2011)
FounderRed Hat
Headquarters
USA
ProductsCeylon programming language
OwnerRed Hat
Websiteceylon-lang.org

Ceylon is an object-oriented programming language with a strong emphasis on static typing and immutability, developed by Red Hat. It operates on the Java virtual machine (JVM) platform and also supports compilation to JavaScript. The language's architecture focuses on enhancing code readability, ensuring predictable behavior, improving tool support, fostering modular design, and enabling advanced metaprogramming capabilities.

Prominent aspects include:

  • A type system that guarantees null safety and verifies list element presence during compilation
  • Uniform syntax and semantics designed to reduce exceptions and irregularities
  • Native support for generics and metaprogramming featuring reified generics
  • Integrated modularity based on JBoss modules, with compatibility for OSGi and Maven
  • Comprehensive tooling, including an IDE built on the Eclipse platform

The name "Ceylon" alludes to Java by referencing the Sri Lankan island formerly called Ceylon, both historically recognized for their coffee and tea production.

In August 2017, the project was contributed to the Eclipse Foundation. Development activity diminished and eventually halted by 2020. By April 2023, the Eclipse Foundation formally concluded the transition process.

Language features

While Ceylon inherits much of its syntax from Java, it introduces several notable improvements.

Type system

One of Ceylon’s key advancements lies in its type system, which replaces Java’s separation between primitive types and boxed objects with a unified model where all types are first-class objects. Although this can introduce some boxing overhead, it streamlines type management.

The language supports both union and intersection types, concepts also found in languages like TypeScript and Whiley, and which later influenced languages such as Flow.

Union types (A|B) permit variables to hold values of different types. For instance, a function can accept either an integer or a string:

<syntaxhighlight lang="ceylon"> shared void integerOrString(Integer|String input) {

   if (is Integer input) {
       print("Got the integer ``input``");
   } else {
       print("Got the string '``input``'");
   }

} </syntaxhighlight>

Intersection types (A&B) enable flow-sensitive typing, allowing variables’ types to be refined based on runtime checks:

<syntaxhighlight lang="ceylon"> shared void integerOrString(Integer|String input) {

   Integer added = input + 6; // illegal; + not defined on Integer|String
   if (is Integer input) {
       Integer added = input + 6; // legal; input is Integer
       print("Got the integer ``input``");
   } else {
       print("Got the string '``input``'");
   }

} </syntaxhighlight>

The expression is Integer input narrows the variable input to type Integer by distributing intersection types and removing incompatible type options.

Null safety

Ceylon employs union and intersection types to provide strong guarantees against null-related errors. Its top-level type, Anything, branches into two distinct subtypes: Object, encompassing all standard classes and interfaces, and Null, which has only one instance: null. Because these two subtypes are mutually exclusive, common types like Integer or List<String> are non-nullable by default. Nullable variants must be explicitly declared as unions, such as Integer|Null or using shorthand like Integer?.

Intersection types allow extraction of non-nullable components from nullable ones, which is particularly useful in generic contexts. For example, a function designed to filter out null values from an iterable might be typed as follows:

<syntaxhighlight lang="ceylon"> Iterable<Element&Object> removeNulls<Element>(Iterable<Element> stream); </syntaxhighlight>

When invoked with a type like Integer|Null, this function returns an iterable containing only non-null Integer elements.

Functions

Ceylon provides support for first-class and higher-order functions, encompassing function types as well as anonymous functions.

<syntaxhighlight lang="ceylon"> // Higher-order function at the top level using block syntax String process(String text, String transformString(String input)) {

   return transformString(text);

}

// Expression-bodied top-level function invoking String.reversed String reverse(String text) => text.reversed;

// Function reference to String.reversed, equivalent to the above String(String) reverseFunctionReference = String.reversed;

// Passing the top-level function as an argument to the higher-order function String reversed1 = process("one", reverse);

// Using an anonymous function as a parameter to the higher-order function String reversed2 = process("one", (text) => text + text); </syntaxhighlight>

Enumerated types

Ceylon implements enumerated types (enums) by restricting the instances of an abstract class to a defined set of singleton objects. Starting from version 1.2, enums can also be defined through named constructors.

<syntaxhighlight lang="ceylon"> // Classic enum pattern limiting instances to three singletons abstract class Vehicle(shared String name) of plane | train | automobile {}

object plane extends Vehicle("plane") {} object train extends Vehicle("train") {} object automobile extends Vehicle("automobile") {} // Compilation error: 'boat' is not a subtype of the enumerated supertype //object boat extends Vehicle("boat") {}

// Enum syntax using named constructors (since Ceylon 1.2) class Vehicle of plane | train | automobile {

   String name;
   abstract new named(String pName) {
       name = pName;
   }
   shared new plane extends named("plane") {}
   shared new train extends named("train") {}
   shared new automobile extends named("automobile") {}
   // Compilation error: 'boat' not included in 'of' clause
   //shared new boat extends named("boat") {}

} </syntaxhighlight>

Type inference

Ceylon enforces strong static typing while offering type inference capabilities. The value keyword infers variable types, and function infers types for functions. For instance, these declarations are interchangeable:

<syntaxhighlight lang="ceylon"> Integer i = 3; value i = 3;

Integer add(Integer i1, Integer i2) {

   return i1 + i2;

} function add(Integer i1, Integer i2) {

   return i1 + i2;

} </syntaxhighlight>

Inference applies only to non-top-level and unshared declarations, enabling single-pass type deduction.

Entry point with names

By default, executing ceylon run triggers the shared run() function within a module:

<syntaxhighlight lang="ceylon"> /* Traditional Hello World example */ shared void run() {

   print("Hello, World!");

} </syntaxhighlight>

Alternatively, any other shared parameterless function may act as the program’s entry point by specifying its name via the run argument:

ceylon run --compile=force --run hello default

Versions

The version timeline for Ceylon spans from M1 0.1 "Newton" (December 2011) up to 1.3.3 "Contents May Differ" (August 2017), representing continuous evolution and feature growth.

License

The Ceylon language is mainly available as free software under the Apache License, with certain portions of its source code governed by the LGPL, facilitating open-source commercial frameworks.

External links