[swift-evolution] Proposal: Optional unwrap resulting in exception.

Nikolay Petrov sungon at gmail.com
Fri Dec 4 15:00:20 CST 2015


Currently, force unwraping .None/nil optional results in a runtime error. I
suggest to put operator/method which will allow the optional unwrap to
result in exception. For example:

enum OptionalError: ErrorType {
    case NoValue(String)
}

extension Optional {
    func unwrap(@autoclosure reason: () -> String = "No value found")
throws -> Wrapped {
        if let val = self {
            return val
        } else {
            throw OptionalError.NoValue(reason())
        }
    }
}

Alternatively we could put a postfix operator like !!.

-- 
Best Regards,
Nikolay Petrov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/befa5b67/attachment.html>


More information about the swift-evolution mailing list