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.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution