type alias
A structurally transparent name for an existing type, declared with type. Wherever the alias
appears in a type position it means its right-hand side.
type ContactId = uint32;
type Coordinate = (float64, float64);
type BearingSeries = float32[];
type MaybeCallsign = Optional<string>;Declaration
An alias is type, a name, =, a type, and a semicolon.
- The name is a global declaration, unique among the file's declarations.
- The right-hand side may be any type, and it resolves in the declaring file's scope.
- A value of the aliased type is assignable where the alias is expected, and the reverse holds. An
alias introduces no type of its own;
newtypeis what does that. - An alias may name another alias, so long as the chain ends somewhere.
type ContactId = uint32;
type Coordinate = (float64, float64);
const radar_id: ContactId = 7;
const station: Coordinate = (42.3601, -71.0589);
radar_id + (1: uint32) // => 8
station._0 // => 42.3601Errors
An alias defined in terms of itself
A cycle is rejected however many aliases it runs through.
type ContactId = TrackId; // error: cyclic-type-alias
type TrackId = ContactId; // error: cyclic-type-aliasAn alias naming something that is not a type
const max_tracks: uint16 = 512;
type Capacity = max_tracks; // error: not-a-typeRelated
- newtype — the nominal wrapper, which is a distinct type.
- primitives — the types an alias usually renames.