It is apparently possible to write x? = y where x: T? and y: T. This is not made clear in the reference or the guide, with the reference merely making a passing reference to this possibility by stating
The unwrapped value of an optional-chaining expression can be modified, either by mutating the value itself, or by assigning to one of the value’s members.
Since all uses of optional-chaining expressions in the book (that I could find) are inside an expression with another subsequent postfix operator, a reader may believe this is the only possible use. Therefore, I suggest adding an explicit notice that this is possible, along with an example.
func bar () -> Int {
return 5
}
do {
var u: Int?
print (u)
u = bar ()
print (u)
}
do {
var u: Int?
print (u)
u? = bar ()
print (u)
}
// Prints
nil
Optional(5)
nil
nil
This should be clearly explained in the book, also along with an example for the super useful, as @tera once remarked, pattern: