[swift-evolution] [Pre-Draft] Nil-coalescing and errors

John McCall rjmccall at apple.com
Thu Apr 7 00:26:51 CDT 2016


> On Apr 6, 2016, at 9:21 AM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>>> Interesting, but I’m unsure if all of it is significantly better than just using the guard that is effectively inside of the operator/func that is being proposed:
>>> 
>>> guard let value = Int("NotANumber") else { throw InitializerError.invalidString }
>>> 
>> 
>> That is a pretty damn compelling argument.
> 
> For some cases, yes. For others…
> 
> 	myInt = Int("NotANumber") ?? throw InitializerError.invalidString
> 
> On the other hand, all we really need is a generalized "noneMap" function marked as rethrowing, which can serve multiple purposes.
> 
> 	myOtherInt = Int("NotANumber").noneMap(arc4random)
> 	myInt = try Int("NotANumber").noneMap { throw InitializerError.invalidString }
> 
> On the gripping hand: I think this is only a problem because `throw` is a statement, not an expression. Could it be changed to be an expression with an unspecified return type? I believe that would allow you to simply drop it into the right side of a ?? operator, and anywhere else you might want it (boolean operators, for instance).

For what it's worth, this is how it was originally designed.  Reducing it to a statement was a simplification to gain consensus.

John.


More information about the swift-evolution mailing list