[swift-evolution] throw expressions in ternary operators
David Hart
david at hartbit.com
Mon May 2 07:25:09 CDT 2016
I remember discussing something like this in swift-evolution but can’t find it. I was wondering if it was worth a proposal or not.
When constructing objects from dictionary of values, I often write the parsing of optional values as such:
age = try dictionary["age"].flatMap {
if let age = $0 as? Int {
return age
} else {
throw Error.InvalidFormat
}
}
Basically, I don’t want to throw if the dictionary does not contain an “age” key, but if it does, I want to throw if it is not an Int. I’m writing this way, but I’d like to write is as such:
age = try dictionary["age"].flatMap { $0 as? Int ?? throw Error.InvalidFormat }
But this causes a compilation error. Don’t you think the ternary operator should allow an expression that throws on the right hand side?
David.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160502/a1eab4a2/attachment.html>
More information about the swift-evolution
mailing list