Allow failable initializers to return .none as well as nil


(Rui Costa) #1

With the introduction of the Optional type in Swift, it is possible to
write an entire application without explicitly referring to nil and instead
use the Optional's .none case. However, on failable initializers nil is the
only return value permitted by the compiler.
Given that the initialization of a type with a failable initializer
generates an Optional, it feels it should be possible to *also* return
.none from a failable initializer as an alternative to nil.

Example:

class Foo {
    let bar: String

    init?(bar: String?) {
        guard let bar = bar else { return .none } // Compile error: nil is
the only return value permitted in an initializer...

        self.bar = bar
    }
}