If a variable or constant declaration is not annotated explicitly with a type, the declaration's type is inferred from the initial value.
Decimal integer literals and hex literals are inferred to type Int
.
1let a = 12// `a` has type `Int`34let b = -455// `b` has type `Int`67let c = 0x028// `c` has type `Int`
Unsigned fixed-point literals are inferred to type UFix64
.
Signed fixed-point literals are inferred to type Fix64
.
1let a = 1.22// `a` has type `UFix64`34let b = -1.25// `b` has type `Fix64`
Similarly, for other basic literals, the types are inferred in the following manner:
Literal Kind | Example | Inferred Type (x) |
---|---|---|
String literal | let x = "hello" | String |
Boolean literal | let x = true | Bool |
Nil literal | let x = nil | Never? |
Array literals are inferred based on the elements of the literal, and to be variable-size. The inferred element type is the least common super-type of all elements.
1let integers = [1, 2]2// `integers` has type `[Int]`34let int8Array = [Int8(1), Int8(2)]5// `int8Array` has type `[Int8]`67let mixedIntegers = [UInt(65), 6, 275, Int128(13423)]8// `mixedIntegers` has type `[Integer]`910let nilableIntegers = [1, nil, 2, 3, nil]11// `nilableIntegers` has type `[Int?]`1213let mixed = [1, true, 2, false]14// `mixed` has type `[AnyStruct]`
Dictionary literals are inferred based on the keys and values of the literal. The inferred type of keys and values is the least common super-type of all keys and values, respectively.
1let booleans = {21: true,32: false4}5// `booleans` has type `{Int: Bool}`67let mixed = {8Int8(1): true,9Int64(2): "hello"10}11// `mixed` has type `{Integer: AnyStruct}`1213// Invalid: mixed keys14//15let invalidMixed = {161: true,17false: 218}19// The least common super-type of the keys is `AnyStruct`.20// But it is not a valid type for dictionary keys.
Ternary expression type is inferred to be the least common super-type of the second and third operands.
1let a = true ? 1 : 22// `a` has type `Int`34let b = true ? 1 : nil5// `b` has type `Int?`67let c = true ? 5 : (false ? "hello" : nil)8// `c` has type `AnyStruct`
Functions are inferred based on the parameter types and the return type.
1let add = (a: Int8, b: Int8): Int {2return a + b3}45// `add` has type `((Int8, Int8): Int)`
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.
1// Invalid: not possible to infer type based on array literal's elements.2//3let array = []45// Instead, specify the array type and the concrete element type, e.g. `Int`.6//7let array: [Int] = []89// Or, use a simple-cast to annotate the expression with a type.10let array = [] as [Int]
1// Invalid: not possible to infer type based on dictionary literal's keys and values.2//3let dictionary = {}45// Instead, specify the dictionary type and the concrete key6// and value types, e.g. `String` and `Int`.7//8let dictionary: {String: Int} = {}910// Or, use a simple-cast to annotate the expression with a type.11let dictionary = {} as {String: Int}