[swift-evolution] ternary operator ?: suggestion

Chris Lattner clattner at apple.com
Wed Dec 16 17:12:50 CST 2015


> On Dec 16, 2015, at 2:56 PM, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Selector Operator Proposal
> 
> OK really trying to get this down to its essence, make something that is truly better and more powerful than ternary, and try to keep as much of the advantages of the ternary and hopefully slightly improve the readability. I am calling it the "selector operator". This proposal, does not add a new keyword but adds a new operator. It unifies the concepts of ternary and gives us a new switch like behavior that ternary does not support. The concept is kind of like a train track, with multiple possible directions selectable by one input. 
> 
> I believe it also addresses the issues that Chris mentioned. 
> 
> to select from a boolean, a or b:
> let a = sel ->> true, false

I’m not sure how I feel about this proposal in general, but if you changed the RHS to take a tuple, you could define it entirely in the library as a binary operator, instead of hacking it into the compiler, like ?: is.

	let a = sel ->> (true, false)

The primary disadvantage of this (which is probably a showstopper!) is that you lose short circuiting.

-Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/8d01226f/attachment.html>


More information about the swift-evolution mailing list