[swift-evolution] ternary operator ?: suggestion

Vester Gottfried vester.gottfried at gmail.com
Thu Dec 17 06:03:25 CST 2015


What do you think about this?

let i = if(x == y, 123, 456)

the second parameter could be optional, so if the condition is false you
would get a nil

let i = if(x == y, 123) // may return nil

making it function like would make a bit more natural to chain other
functions

let i = if(x == y, 123).map( ... )

I am usually not an opponent of the ternary conditional operator, but I do
know instances where beginner were struggling with it, when they first
encountered it. That you cannot google it makes it worse. And in Swift the
question mark has a special meaning regarding optionals, that other
languages don't have.

This is just an quick idea though. I agree with Chris, that we should only
replace it, when we are really sure the new solution is better.


On Thu, Dec 17, 2015 at 4:36 AM, Charles Constant via swift-evolution <
swift-evolution at swift.org> wrote:

> One more thought. This syntax would also placate ternary haters. I think
> the traditional complaint about ternary expressions is that people can't
> remember the order of the true and false values. This would not be an issue
> with:
>
>  let i = boo ? ( true: 123, false: 456 )
>
>
>
> _______________________________________________
> 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/20151217/bc815bfb/attachment.html>


More information about the swift-evolution mailing list