I created this set of extensions so I could more easily deal with optionals in
a context where I was already potentially throwing errors.
extension Optional {
func deoptionalize() throws -> Wrapped {
switch self {
case .some(let wrapped): return wrapped
case .none: throw "\(self.self) was nil."
}
}
}
extension String: LocalizedError {
public var localizedDescription: String {
return self
}
}
It’s probably a bad idea to make String an Error, but it worked. It seems to me
like there should be something more convenient for dealing with Optionals in
contexts where unwrapping them is awkward.
Jon
> On Feb 9, 2017, at 5:35 PM, Rob Mayoff via swift-evolution
> <[email protected]> wrote:
>
> On Thu, Feb 9, 2017 at 2:25 PM, Haravikk via swift-evolution
> <[email protected] <mailto:[email protected]>> wrote:
> I wonder if an alternative to the original proposal might be to allow throw
> on the right hand side? So you could do:
>
> let y = x ?? throw myError
>
>
> You can do this today:
>
> extension Error {
> func throwMe<R>() throws -> R { throw self }
> }
>
> let y = try x ?? MyError().throwMe()
>
> _______________________________________________
> swift-evolution mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution