Boolean Literals are the easiest to understand: There is only
false. The integer equivalents for these are
1(or anything that is not
The type of Boolean Literals is always inferred to
The Null Literal
nullbehaves exactly the same as in Java. It can be used in placed of any Nullable Type.
let obj: Object? = null
let string: String! = null
let dates: [Date]? = null
let array: [int]! = null
let i: int = null // error - int is a primitive type
The type of the Null Literal is inferred to the Null Type, which uses the same keyword.
var n = null
var n: null = null
Note that it is not possible to assign anything other than
nullto such a variable. Thus, the compiler will reject the following code:
var n = null // n: null
n = "abc" // error - cannot assign java.lang.String to null
Another special Literal Value is the Wildcard Literal
_. It can be used in places where you want the default value of a type to be passed. The following table shows which value the Wildcard Literal will represent based on the type:
Because the value of the Wildcard Literal depends on the context, it's type cannot be inferred. Any attempt to do so will result in a compilation error. Since non-primitive non-nullable values cannot have a default value, it is a compile-time error to assign a wildcard literal to such a type.