Supertypes, Subtypes, & Type Coercion
Types are able to automatically be coerced into their supertypes.
main {
let bar: uint = 10;
let foo: int = bar; // `uint` is a subtype of `int`, so it can be coerced to type `int`
}
Array Types
An array type A is a subtype of another array type B if:
- The element type of
Ais a subtype of the element type ofB - The two array types are the same length
main {
let ints: [int; 5] = [1, 2, 3, 4, 5];
let nums: [num; 5] = ints;
}
Struct Types
A struct type A is a subtype of another struct type B if:
A&Bshare the same number of fields, with the same names, in the same order- The type of a given field in
Ais a subtype of that same field's type inB
main {
let coords: { x: num, y: num } = { x: 1.1, y: -2 };
let vals: { x: val, y: val } = coords;
}
Reference Types
A reference type &A is a subtype of another reference type &B only if A & B are isomorphic, meaning A is a subtype of B and B is a subtype of A.