Types and type declarations

Usage

A type declaration introduces a new type or type constructor.

class MyClass(name) {
    shared String name;
}
interface MyInterface() {
    shared formal String name;
}
object myObject {
    shared String name = "Trompon";
}

Description

Ceylon is a statically typed language. This means that the type of every expression is known at compile time and the compiler uses this information to prevent typing errors.

Moreover the type system is based on principal types, which means every expression has a unique, most specific type, expressible within the language.

Types are formed by supplying type argument lists, and recursively applying the union and intersection operations, to type declaration or type alias references.

Type declarations

In Ceylon, a type declaration is one of:

A type parameter is also sometimes considered a kind of type declaration. However, a type parameter may not declare members.

Member declarations

A declaration that occurs directly in a type declaration is called a member declaration. Member values are called attributes. Member functions are called methods.

Local declarations

A local (or nested) declaration is a declaration that is contained within another declaration or a statement.

Top-level declarations

A top level declaration is contained directly in a compilation unit and not contained within any other declaration. In other words a top level declaration is neither a member nor a local declaration.

Union types

Given type expressions X and Y, Ceylon lets you express the union of instances of those types using the notation X|Y. For example:

variable String|Integer x = "";
x = 0;

Intersection types

Given type expressions X and Y, Ceylon lets you express the intersection of instances of those types using the notation X&Y.

Enumerated types

Classes can enumerate a list of their permitted subclasses.

Interfaces can enumerate a list of their permitted subtypes.

Type aliases

To avoid having to repeat long type expressions, you can define a type alias for a type using the alias keyword:

alias BasicType = String|Character|Integer|Float|Boolean;

Null

Null is the type of null. If an expression permits null then it needs Null as a supertype. This is usually expressed as using a union type such as T|Null, which can be abbreviated as T?.

Nothing

Nothing is the intersection of all types. It is equivalent to the empty set. Because Nothing is the intersection of all types it is assignable to all types. Similarly because it is the intersection of all types it can have no instances. It corresponds to the notion of an empty set in mathematics.

Iterable

Iterable is a type that produces instances of another type when iterated. Iterable<T> is usually abbreviated to {T*}.

Sequential

Sequential is an enumerated type with subtypes Sequence and Empty. Sequential<T> is usually abbreviated to T[] or [T*].

Empty

Empty is the type of the expression [].

Sequence

Sequence is the type of non-empty sequences. Sequence<T> is usually abbreviated to [T+].

Tuple

Tuple is a subclass of Sequence (and thus cannot be empty). It differs from Sequence in that it encodes the types of each of its elements individually.

[Integer, Boolean, String] t = [1, true, ""];
Integer first = t[0];
Boolean second = t[1];
String last = t[2];

Tuples also have a notion of 'variadicity':

// A tuple of at least two elements
// the first is an Integer and 
// the rest are Boolean
[Integer, Boolean+] t = [1, true, false];
// A tuple of at least element
// the first is an Integer and 
// the rest are Boolean
[Integer, Boolean*] t2 = t;

Tuple types may thus be used to represent the type of an argument or parameter list, and are therefore used to encode function types.

Unabbreviated tuple types are extremely verbose, and therefore the abbreviated form is always used.

Metamodel

TODO

See also