[swift-evolution] ternary operator ?: suggestion

Adrian Kashivskyy adrian.kashivskyy at me.com
Fri Dec 4 11:17:23 CST 2015


-1 on Bool extension, this should exist as a free function. Reason?

(1 + 1 == 2).ifTrue("foo", otherwise: "bar")

This: 1. doesn't look well; 2. is not clear and understandable; 3. doesn't allow condition to be a closure.


Pozdrawiam – Regards,
Adrian Kashivskyy

> Wiadomość napisana przez Sean Heber <sean at fifthace.com> w dniu 04.12.2015, o godz. 16:54:
> 
> Could alternatively could take a page from Smalltalk:
> 
>   extension Bool {
>      func ifTrue<T>(@autoclosure isTrue: () -> T, @autoclosure otherwise isFalse: () -> T) -> T {
>         if self {
>            return isTrue()
>         } else {
>            return isFalse()
>         }
>      }
>   }
> 
> :)
> 
> l8r
> Sean
> 
> 
>> On Dec 4, 2015, at 1:40 AM, David Waite <david at alkaline-solutions.com> wrote:
>> 
>> Of course one could just drop it as a grammatical language feature and turn it a standard library feature of some form, e.g.:
>> 
>> @warn_unused_result
>> func when<T>(@autoclosure expr: ()->Bool, @autoclosure value: ()->T, @autoclosure otherwise: ()->T) -> T {
>>  if expr() {
>>    return value()
>>  }
>>  else {
>>    return otherwise()
>>  }
>> }
>> 
>> var a = when(1 + 1 == 2, value: "works", otherwise: "fails")
>> 
>> print(a) // => “works"
>> 
>> -DW
>> 
>>> On Dec 4, 2015, at 12:26 AM, 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
>> 
> 
> _______________________________________________
> 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/1fc6baf1/attachment.html>


More information about the swift-evolution mailing list