[swift-evolution] ternary operator ?: suggestion

Chris Lattner clattner at apple.com
Wed Dec 16 23:26:24 CST 2015


> On Dec 16, 2015, at 4:36 PM, Paul Ossenbruggen <possen at gmail.com> wrote:
> 
> Hi Chris,
> 
> So, just trying to understand if you think this is a good direction to continue with or you think it is inherently flawed. 
> 
> Can you give me an example of a function argument list or array separator that we would have a problem? I will see if I can address it.

Transform this into your proposed syntax.
func foo(a : Int, b : Float) {}
foo(true ? 1 : 2, 3)

-Chris

> 
> Thanks for the feedback!
> 
> - Paul
> 
> 
>> On Dec 16, 2015, at 3:14 PM, Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>> 
>> On Dec 16, 2015, at 2:56 PM, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto: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/7e8c549a/attachment.html>


More information about the swift-evolution mailing list