Type Inference
🚧 Status: Only basic type inference is implemented.
If a variable or constant declaration is not annotated explicitly with a type, the declaration's type is inferred from the initial value.
Integer literals are inferred to type Int.
let a = 1
// `a` has type `Int`Array literals are inferred based on the elements of the literal, and to be variable-size.
let integers = [1, 2]
// `integers` has type `[Int]`
// Invalid: mixed types
//
let invalidMixed = [1, true, 2, false]Dictionary literals are inferred based on the keys and values of the literal.
let booleans = {
1: true,
2: false
}
// `booleans` has type `{Int: Bool}`
// Invalid: mixed types
//
let invalidMixed = {
1: true,
false: 2
}Functions are inferred based on the parameter types and the return type.
Type inference is performed for each expression / statement, and not across statements.
There are cases where types cannot be inferred. In these cases explicit type annotations are required.
Last updated
Was this helpful?