[swift-evolution] ternary operator ?: suggestion

Charles Constant charles at charlesism.com
Tue Dec 15 16:06:31 CST 2015


+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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/cfe36685/attachment.html>


More information about the swift-evolution mailing list