Inkling types
Inkling is a statically typed language, which means that each variable, constant, and parameter must have a well-defined type.
Primitives
number: an arithmetical value.string: a sequence of one or more unicode characters.
Collections
- Array: a non-zero-length array of same-typed values.
- Structure: a collection of typed fields with unique names.
Inkling uses structural typing to determine equivalency between types. If the
shape of the types matches, the types are considered equivalent. For example, the
following types are equivalent, even though they have different names, because
they are both type number:
type Type1 number
type Type2 number
User-defined types
Use the type statement to create custom types. Custom types can be used
anywhere a type literal would be used.
type Height number
type Coordinate {X: number, Y: number}
type TicTacToeValue number<Blank=0, X=1, O=2>
type TicTacToeBoard TicTacToeValue[3][3]
Implicit casting
The Inkling compiler validates that values assigned to variables and parameters are compatible with the assigned type. Compatibility does not require an exact type match, it only requires:
- the change from one type to the other has no loss of precision.
- the value is not converted to an enumeration.
Important
The Inkling compiler always assumes the result of a mathematical operation is a number. As a result, the compiler cannot statically evaluate mathematical operations that use variables.
Assigning a number to an enumerated constraint like number<1, 2> is not
allowed because the compiler cannot verify that the result belongs to the
enumerated set.
Casting to a range constraint like number<1 .. 2> is allowed, but the
compiler will warn that the result may be rounded to fit within the range.
For more information on range and enumeration constraints, see the Number type.
Examples of valid assignments
The following example works because the value of OneOrTwo is guaranteed to
fit within the range assigned to ZeroTo100.
var OneOrTwo: number<1, 2> = 1 + 1 # Enumerated constraint
var ZeroTo100: number<0..100> = 20 # Range constraint
ZeroTo100 = OneOrTwo
The following example also works, but generates a warning that the assignment
may require rounding. The compiler cannot evaluate the value of 1 + OneOrTwo
statically to ensure it results in a valid range value for ZeroTo2.
var OneOrTwo: number<1, 2> = 1 + 1
var ZeroTo2: number<0 .. 2> = 1 + OneOrTwo
Examples of invalid assignments
The following example generates a compiler error. The value of ZeroTo100 may
exceed the permitted values of the range type of ZeroTo50.
var ZeroTo50: number<0..50> = 50
var ZeroTo100: number<0..100> = 20
ZeroTo50 = ZeroTo100
For more details about casting compatibility, see the relevant type documentation.