[swift-evolution] ternary operator ?: suggestion

Charles Constant charles at charlesism.com
Wed Jan 6 08:19:11 CST 2016

Hi John,

I realize the Swift philosophy is puposefully against brevity at the
expense of clarity, but to my eye a switch - for the task we're discussing
is verbose enough to be more confusing.

**** Proposal ****

let val = color ? ( .Red ! 0xFF0000 ) : ( .Green ! 0x00FF00 ) : ( .Blue !
0x0000FF ) : ( _ ! 0xFFFFFF )

**** Existing ****

let val:Int

switch color {

case .Red: val = 0xFF0000

case .Green: val = 0x00FF00

case .Blue: val = 0x0000FF

default: val = 0xFFFFFF



If I'm revisiting my code, I'd prefer to reread the short version. It's not
just easier to write, but also probably easier to read. Of course, there's
no empirical evidence unless we sit down a bunch of programmers and do a
study. So that's just my opinion.

On Wed, Jan 6, 2016 at 6:07 AM, John Randolph <jcr at icloud.com> wrote:

> Really not a fan of the idea of bloating the ternary operator out to deal
> with more cases.  That’s what we already have switch and enum for.
> -jcr
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160106/4344ddfe/attachment.html>

More information about the swift-evolution mailing list