AnyStruct, AnyResource, Optionals, and Never
AnyStruct
is the top type of all non-resource types (i.e., all non-resource types are a subtype of it).
AnyResource
is the top type of all resource types.
_37// Declare a variable that has the type `AnyStruct`._37// Any non-resource typed value can be assigned to it, for example an integer,_37// but not resource-typed values._37//_37var someStruct: AnyStruct = 1_37_37// Assign a value with a different non-resource type, `Bool`._37someStruct = true_37_37// Declare a structure named `TestStruct`, create an instance of it,_37// and assign it to the `AnyStruct`-typed variable_37//_37struct TestStruct {}_37_37let testStruct = TestStruct()_37_37someStruct = testStruct_37_37// Declare a resource named `TestResource`_37_37resource TestResource {}_37_37// Declare a variable that has the type `AnyResource`._37// Any resource-typed value can be assigned to it,_37// but not non-resource typed values._37//_37var someResource: @AnyResource <- create TestResource()_37_37// Invalid: Resource-typed values can not be assigned_37// to `AnyStruct`-typed variables_37//_37someStruct <- create TestResource()_37_37// Invalid: Non-resource typed values can not be assigned_37// to `AnyResource`-typed variables_37//_37someResource = 1
However, using AnyStruct
and AnyResource
does not allow you to opt out of type checking. It is invalid to access fields and call functions on these types, as they have no fields and functions.
_10// Declare a variable that has the type `AnyStruct`._10// The initial value is an integer,_10// but the variable still has the explicit type `AnyStruct`._10//_10let a: AnyStruct = 1_10_10// Invalid: Operator cannot be used for an `AnyStruct` value (`a`, left-hand side)_10// and an `Int` value (`2`, right-hand side)._10//_10a + 2
AnyStruct
and AnyResource
may be used like other types. For example, they may be the element type of arrays or be the element type of an optional type.
_13// Declare a variable that has the type `[AnyStruct]`,_13// i.e. an array of elements of any non-resource type._13//_13let anyValues: [AnyStruct] = [1, "2", true]_13_13// Declare a variable that has the type `AnyStruct?`,_13// i.e. an optional type of any non-resource type._13//_13var maybeSomething: AnyStruct? = 42_13_13maybeSomething = "twenty-four"_13_13maybeSomething = nil
AnyStruct
is also the super-type of all non-resource optional types, and AnyResource
is the super-type of all resource optional types.
_10let maybeInt: Int? = 1_10let anything: AnyStruct = maybeInt
Conditional downcasting allows coercing a value that has the type AnyStruct
or AnyResource
back to its original type.
Optionals
Optionals are values which can represent the absence of a value. Optionals have two cases: either there is a value or there is nothing.
An optional type is declared using the ?
suffix for another type. For example, Int
is a non-optional integer and Int?
is an optional integer (i.e., either nothing or an integer).
The value representing nothing is nil
.
_17// Declare a constant which has an optional integer type,_17// with nil as its initial value._17//_17let a: Int? = nil_17_17// Declare a constant which has an optional integer type,_17// with 42 as its initial value._17//_17let b: Int? = 42_17_17// Invalid: `b` has type `Int?`, which does not support arithmetic._17b + 23_17_17// Invalid: Declare a constant with a non-optional integer type `Int`,_17// but the initial value is `nil`, which in this context has type `Int?`._17//_17let x: Int = nil
Optionals can be created for any value, not just for literals.
_15// Declare a constant which has a non-optional integer type,_15// with 1 as its initial value._15//_15let x = 1_15_15// Declare a constant which has an optional integer type._15// An optional with the value of `x` is created._15//_15let y: Int? = x_15_15// Declare a variable which has an optional any type, i.e. the variable_15// may be `nil`, or any other value._15// An optional with the value of `x` is created._15//_15var z: AnyStruct? = x
A non-optional type is a subtype of its optional type.
_10var a: Int? = nil_10let b = 2_10a = b_10_10// `a` is `2`
Optional types may be contained in other types (e.g., arrays or even optionals.)
_10// Declare a constant which has an array type of optional integers._10let xs: [Int?] = [1, nil, 2, nil]_10_10// Declare a constant which has a double optional type._10//_10let doubleOptional: Int?? = nil
See the optional operators section for information on how to work with optionals.
Never
Never
is the bottom type (i.e., it is a subtype of all types). There is no value that has type Never
.
Never
can be used as the return type for functions that never return normally. For example, it is the return type of the function panic
.
_17// Declare a function named `crashAndBurn` which will never return,_17// because it calls the function named `panic`, which never returns._17//_17fun crashAndBurn(): Never {_17 panic("An unrecoverable error occurred")_17}_17_17// Invalid: Declare a constant with a `Never` type, but the initial value is an integer._17//_17let x: Never = 1_17_17// Invalid: Declare a function which returns an invalid return value `nil`,_17// which is not a value of type `Never`._17//_17fun returnNever(): Never {_17 return nil_17}