Types are explicit in Ceylon source code at every declaration that is not inferred.
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.
In Ceylon, a type declaration is one of:
A local (or nested) declaration is a declaration that is contained within another declaration or a statement.
Given type expressions
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;
Given type expressions
Y, Ceylon lets you express the
intersection of instances of those types using the notation
Classes can enumerate a list of their permitted subclasses.
Interfaces can enumerate a list of their permitted subtypes.
You can define an alias for a type using the
alias BasicType = String|Character|Integer|Float|Boolean;
interface Strings = Collection<String>;
Null is the type of
null. If an expression permits
null then it
Null as a supertype. This is usually expressed as using a
union type such as
T|Null, which can be abbreviated
Nothing is the intersection of all types. It is equivalent to the empty set.
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
Sequential is an enumerated type with subtypes
Sequential<T> is usually abbreviated to
Empty is the type of the
Sequence is the type of non-empty
Tuple is a subclass of
Sequence (and thus cannot be empty). It differs from
Sequence in that the typechecker knows types of each of its elements
[Integer, Boolean, String] t = [1, true, ""]; Integer first = t; Boolean second = t; String last = t;
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;
Tuples thus have the same assignability rules as do
positional argument list and parameter lists.
Unabreviated tuple types are extremely verbose, and therefore the abbreviated form is strongly preferred.