[swift-evolution] Allow failable initializers to return .none as well as nil

Rui Costa rui.pfcosta at gmail.com
Sun Aug 28 09:07:33 CDT 2016


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
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160828/3298154a/attachment.html>


More information about the swift-evolution mailing list