Aliases

A type alias can give a new name to an existing type, whether that type is declared in the same project, or elsewhere.

Type aliases can serve many purposes. They can provide a short-hand for an otherwise more complex to write type (such as Array or a Tuple with a specific configuration) or they can distinguish between different use cases of the same base type (such as declaring different, incompatible aliases to Double refer to different units of measure.

Type aliases can be declared by simply following the equal sign with the name of another type, optionally preceded by a Visibility Level.

type
  Number = public Integer; // "Number" now has the same meaning as "Integer"

Incompatible Type Aliases

A type alias can be marked to be deliberately incompatible with the original type (and other aliases to the same type) by applying the type Type Modifier:

type
  Celsius = type Double;
  Farenheit = type Double;
  
var BoilingTemp: Celsius = 100.0;

var OvenTemperature: Fahrenheit := BoilingTemp; // this assignment will fail with an error.

You could even provide Custom Operators for the new incompatible types (as a global, or via an Extension that could handle conversions (where applicable), so that you could then write


operator Explicit(C: Celsius): Fahrenheit;
begin
  result := (C * 9.0/5.0) + 32.0
end;

...

var OvenTemperature: Fahrenheit := Fahrenheit(BoilingTemp);

Combined Interfaces

A type alias can combine two or more Interfaces into a new Interface type, using the and keyword:

type
  ICodable = IEncodable and IDecodable;

To be compatible with the new combined interface type, a type must implement all the interfaces, and declaring a type to implement the combined interface automatically marks it as implement (and requires it to implement) all the interfaces.

See Also