Proposal: Optional unwrap resulting in exception.


(Nikolay Petrov) #1

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