[swift-evolution] ternary operator ?: suggestion

Paul Ossenbruggen possen at gmail.com
Tue Dec 15 18:31:26 CST 2015


Been thinking a bit:

Perhaps a new expression is in order. “Pick” this has a form like this. Param is a selector  This only allows expressions 

It has two forms: 

To replace ternary: 

let x = pick val from "abc", "cdef"

To replace switch expressions. The cases follows existing rules for switch cases. 

let y = pick val from cases .Red: 1, .Green: 2, .Blue: 3

This keeps the notion of expressions and statements quite separate. It avoids syntax confusion. It reads clear. It is fairy concise. It uses a straight forward pattern for both forms of expression. 

- Paul

> On Dec 15, 2015, at 2:06 PM, Charles Constant via swift-evolution <swift-evolution at swift.org> wrote:
> 
> +1 bigtime for the assignment via Switch proposal
> 
> I think someone here made the argument, I can't remember who, that it would be confusing for beginners. I think exactly the opposite. 
> 
> Once a new programmer has learned how to write a normal Switch statement, they'll be able to "leverage" the same concept and map values using the Switch assignment. Some might even try it on their on own, through experimentation, to see if it will work. It's such a pleasant experience when you try something in a language that seems consistent with what you already know, and discover "cool, it works!"
> 
> At the moment, the alternatives are, what, using a dict to map values? trying to shoehorn a corrsponding set of values into an enum? using the existing switch statement (pretty verbose in Swift, due to "let" scope etc)? In my own Swift code, I have encountered situations, frequently, where I wished I had an equivalent to a ternary condition that handled more than two values. Chaining multiple ternary conditions together is unreadable. This proposed Switch assignment expression would take care of that. 
> 
> Definitely has my vote!
> 
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/d2175449/attachment.html>


More information about the swift-evolution mailing list