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

Brent Royal-Gordon brent at architechies.com
Wed Apr 6 11:21:14 CDT 2016


>> 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).

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list