[swift-evolution] ternary operator ?: suggestion

Chris Lattner clattner at apple.com
Wed Dec 16 17:14:31 CST 2015


On Dec 16, 2015, at 2:56 PM, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org> wrote:
> I believe it also addresses the issues that Chris mentioned. 
> 
> to select from a boolean, a or b:
> let a = sel ->> true, false
> 
> to select from an enum values as in a switch:
> 
> let a = sel ->> .Red: 1, .Green: 2, .Blue: 3 
> let b = sel ->> .Red: 1, .Green: 2, .Blue: 3, default: 4
> let c = sel ->> case .Red: 1, case .Green: 2, case .Blue: 3, default: 4
> let d = sel ->> .Red: 1, (sel ->> .Sun: .Yellow, .Moon: .White, .Stars: .Twinkle), .Green: 2, .Blue: 3, default: 4
> let e = sel ->> cases: .Red: 1, case .Green: 2, case .Blue: 3, default: 4
> 
> Disadvantages:
> • May be a slight improvement in readability. Operators have to be looked up if you are not familiar with them. 
> • New concept that will have to be learned and not present in most C like languages so needs to be learned. 

This specific proposal has another problem.  Since you’re using “,” you’ve introduced grammar problems.  For example, you wouldn’t be able to use this operator in a function argument list or array literal, because the , would be parsed as part of the argument list separator.

-Chris

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


More information about the swift-evolution mailing list