[swift-evolution] ternary operator ?: suggestion

J. Cheyo Jimenez cheyo at masters3d.com
Fri Dec 4 13:43:40 CST 2015


I'd like some feedback from the core team to see if eliminating the ternary
operator and replacing it with if-else is even proposal worthy.

Some interesting code in the standard library that uses the ternary
operator quite a bit.

https://gist.github.com/masters3d/7b0c6f3653368afe4357



On Fri, Dec 4, 2015 at 12:50 PM, J. Cheyo Jimenez <cheyo at masters3d.com>
wrote:

> The motive is to desist the use of the question mark outside of the swift
> optional context.
> I like the rust style if-expression as an alternative.
> Imo consistency and readability is a little more important than brevity.
>
>
> On Friday, December 4, 2015, David Hart <david at hartbit.com> wrote:
>
>> Best proposition since, but still more verbose than the ternary operator.
>>
>> On 04 Dec 2015, at 18:27, David Owens II <david at owensd.io> wrote:
>>
>> There’s no need for the “then" keyword if you just allow if-expressions
>> as well.
>>
>> ```
>> let result = if condition { 1 } else { 2 }
>> ```
>>
>> Or more verbosely:
>>
>> ```
>> let result =
>>     if condition {
>>         return 1
>>     }
>>     else {
>>         return 2
>>     }
>> ```
>>
>> The same could be for switch statements as well.
>>
>> ```
>> let result = switch item {
>>     case foo: 1
>>     case bar: 2
>> }
>> ```
>>
>> The above assume implicit returns on single-line statements on the
>> condition/case branches.
>>
>> -David
>>
>> On Dec 3, 2015, at 11:26 PM, David Hart <david at hartbit.com> wrote:
>>
>> I also tend to agree but I don't like the ordering in the Python version
>> and would rather see the introduction of the then keyword
>>
>> On 04 Dec 2015, at 06:08, Sean Heber <sean at fifthace.com> wrote:
>>
>> I agree that using ? for ternary feels.. uncomfortable in Swift. I
>> wouldn't mind seeing Python-style or just eliminating it entirely in favor
>> of simplicity.
>>
>> l8r
>> Sean
>>
>> On Dec 3, 2015, at 10:48 PM, Dan Appel <dan.appel00 at gmail.com> wrote:
>>
>> I agree that ternary can make code unreadable, especially so with the
>> ‘?’. I personally like the Haskel option the most, but it introduces a new
>> keyword ‘then’, which isn’t great either. Python’s "y if x else z" is
>> therefore, in my opinion, the best alternative.
>>
>> Also, I found it interesting how split the Rust community was about
>> removing the ternary operator.
>>
>>>> Dan Appel
>>
>> On Thu, Dec 3, 2015 at 7:58 PM J. Cheyo Jimenez <cheyo at masters3d.com>
>> wrote:
>>
>>> Would you consider replacing the C style ?: ternary operator to
>>> something that does not use the question mark and colon ?
>>>
>>> The use of "?" can be confusing when speed reading code with optionals.
>>>
>>> Consider this code as somebody learning swift as their first language.
>>>
>>> let result = !condition ? 1 : 2
>>>
>>> Some alternatives:
>>>
>>> Haskel
>>>
>>> if predicate then expr1 else expr2
>>>
>>> Python
>>>
>>> result = x if a > b else y
>>>
>>> The advantage of not using the question mark is that the language will
>>> be more consistency on the use of "?" to mean only optionals.
>>>
>>>
>>> References:
>>> https://devforums.apple.com/message/1020139#1020139
>>> https://github.com/rust-lang/rust/issues/1698#issuecomment-3705066
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>> _______________________________________________
>> 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/20151204/023b991e/attachment.html>


More information about the swift-evolution mailing list