[swift-evolution] Brainstorming: Optional sugar inferred map

Brent Royal-Gordon brent at architechies.com
Tue Feb 16 01:13:34 CST 2016


> To address this, the nil-coalescing operator would allow $$, where $$ is the unwrapped unnamed result of the expression when non nil: 
> 
> func doSomething(value: Int) -> Int {
>     return value
> }
> 
> let ra = a ?? doSomething($$) 
> let rc = c ?? doSomething($$)
> 
> ra -> 5
> rc -> nil

I think this is completely the wrong way to approach this.

*If* you want to implement this feature, I think the way to do it is to say that you can put ? after any optional parameter to a normal function call, and Swift will unwrap all the parameters so marked and make the call return `nil` if any of them are nil.

	let ra = doSomething(a?)
	let rc = doSomething(c?)

Naturally, since operators are just function calls with a funny syntax, this would also extend to operators.

	a? + b	// a.k.a. +(a?, b)

Even in this form, however, I don't think this is a feature worth having. Too indirect and specialized.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list