[swift-evolution] ternary operator ?: suggestion

John Joyce uchuugaka at icloud.com
Tue Jan 5 05:11:47 CST 2016


I still find this backward. 
Maybe from years of other languages, except Ruby where this would be a one liner. 

Usually the conditional is before the assignment and that's how it is executed AFAIK

Sent from my iPhone

> On Jan 5, 2016, at 4:16 PM, Charles Constant via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Thanks Austin,
> 
> let myThing = {
>   if someCondition {
>     return 1
>   } else {
>     return 2
>   }
> }()
> 
> While that is ugly, it does have the benefit of being crystal clear about its intent (i.e.: setting "myThing") since everything after the first line is going to be indented.
> 
> What I dislike about using a "switch" to achieve the same thing is rereading my code. It can take a couple moments to clue in that its just assigning, and to realize there's nothing *else* going on in the "switch" aside from the assignment. 
> 
> 
> 
>> On Mon, Jan 4, 2016 at 11:04 PM, Austin Zheng <austinzheng at gmail.com> wrote:
>> There was a popular pattern which went as follows:
>> 
>> let myThing = {
>>   if someCondition {
>>     return 1
>>   } else {
>>     return 2
>>   }
>> }()
>> 
>> It's a little ugly, but the compiler is apparently smart enough to inline/optimize away the anonymous closure. It became less popular once Swift got support for declaring lets without an initial value.
>> 
>> Austin
>> 
>>> On Jan 4, 2016, at 10:55 PM, Charles Constant via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> My best guesses here, since I didn't write and don't entirely agree...
>>> 
>>> > Please detail what the trade offs are
>>> 
>>> Other than more complexity, I think this refers to making the "switch" statement do two slightly different things. Of course, if we call it something else, like "match" or "which" then it stops being an issue.
>>> 
>>> > What are the other ways that Swift currently supports this?
>>> 
>>> As far as I can see, the closest we get are...
>>> 
>>>     a) The existing "switch" statement, or if else statement (preceded with a statement to declare the variable. yuck). I think this is verbose enough to qualify as "confusing"
>>> 
>>>     b) Creating an anonymous dict, and immediately accessing it (which means the keys need to be Hashable). This isn't very flexible
>>> 
>>>     c) Creating a special init or function, to map one enum, to another. This  is also verbose, and not flexible, and moves the code away from where it used (even if you never reuse the mapping).
>>> 
>>>     d) Chaining a bunch of conditions in a ternary... Horrible.
>>> 
>>> Anyone know of any better alternatives? It feels like I'm missing something.
>>> 
>> 
> 
> 
> _______________________________________________
> 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/20160105/8672217c/attachment.html>


More information about the swift-evolution mailing list