[swift-evolution] Nil-rejection operator

Brent Royal-Gordon brent at architechies.com
Thu Feb 9 01:56:05 CST 2017

> On Feb 8, 2017, at 12:00 PM, Jack Newcombe via swift-evolution <swift-evolution at swift.org> wrote:
> I propose the introduction of a nil-rejection operator (represented here as !!) as a complement to the above operators.
> .
> This operator should allow an equivalent behaviour to the forced unwrapping of a variable, but with the provision of an error to throw in place of throwing a fatal error.
> - value !! Error :
> 	if value is nil, throw non-fatal error
> 	if value is not nil, return value
> Example of how this syntax might work (Where CustomError: Error):
> 	let value = try optionalValue !! CustomError.failure

Rather than invent a new operator, I'd prefer to make `throw` an expression rather than a statement. Then you could write:

	let value = optionalValue ?? throw CustomError.Failure

One issue here would be figuring out the proper return type for `throw`. Although if `Never` were a subtype-of-all-types, that would of course work. :^)

Brent Royal-Gordon

More information about the swift-evolution mailing list