Note: information on this page refers to Ceylon 1.2, not to the current release.
Type aliases
A type alias assigns a name to a type expression.
Usage
A type alias:
alias BasicType => String|Character|Integer|Float|Boolean;
A generic type alias:
alias ListOrMap<Element> => List<Element>|Map<Integer,Element>;
An interface
alias:
interface Strings => Collection<String>;
A class
alias:
class Identifier({Character*} characters) => String(characters);
Description
Type aliases allow us to avoid having to repeat long or complex type expressions.
Type aliases are not type declarations as such, and are not reified at runtime. Instead, a type alias is replaced (recursively) by its definition as part of the compilation process.
The different kinds of alias declaration have slightly different semantics, affecting where the alias can appear:
- an interface alias may appear anywhere an interface type may appear,
- a class alias may appear anywhere a class type may appear, and
- a type alias declared using the keyword
alias
may only appear where an arbitrary type expression may appear.
Metamodel
Type aliases can be manipulated at runtime via their representation as
AliasDeclaration
instances. There is no corresponding model interface.
Class and interface aliases are treated as normal classes and interfaces,
therefore their declarations are
ClassDeclaration
and
InterfaceDeclaration
.
The
ClassOrInterfaceDeclaration.isAlias
attribute is used to distinguish aliases from normal classes and interfaces.
The corresponding models are
Class
and
Interface
.
See also
- Type alias elimination, Type aliases, Interface aliases, and Class aliases in the Ceylon language specification
-
import
statements allow the imported declaration to be renamed within the compilation unit. This is usually called an import alias.