[swift-evolution] Control Flow Expressions

David Hart david at hartbit.com
Sat Dec 5 09:56:18 CST 2015


The ternary operator is indeed equivalent to an if expression but:

1) There is no equivalent for switch.
2) There is also a proposition to remove the ternary operator in favour of a if expression.

David.

> On 05 Dec 2015, at 16:41, Jonathan Hull <jhull at gbis.com> wrote:
> 
> Sorry, that should be:
> 
> let message:String = (x % 2 == 0) ? “x is even” : “x is odd”
> 
>> On Dec 5, 2015, at 7:35 AM, Jonathan Hull <jhull at gbis.com <mailto:jhull at gbis.com>> wrote:
>> 
>> Don’t we already have the following?
>> 
>> let message:String = (x % 2) ? “x is even” : “x is odd"
>>  
>> 
>>> On Dec 5, 2015, at 7:02 AM, J. Cheyo Jimenez <cheyo at masters3d.com <mailto:cheyo at masters3d.com>> wrote:
>>> 
>>> Hi Sergey,
>>> 
>>> Are you thinking on writing the formal proposal for the if expression? 
>>> 
>>> https://lists.swift.org/pipermail/swift-evolution/2015-December/000165.html <https://lists.swift.org/pipermail/swift-evolution/2015-December/000165.html>
>>> 
>>> 
>>> On Friday, December 4, 2015, Sergey Shulepov <s.pepyakin at gmail.com <mailto:s.pepyakin at gmail.com>> wrote:
>>> Hello.
>>> 
>>> It would be cool if control flow statements like "switch", "if" can be used as expressions, such as in languages like Scala, Rust and Kotlin,
>>> so instead of writing:
>>> 
>>> var message: String
>>> if x % 2 == 0 {
>>>  message = "x is even"
>>> } else {
>>>  message = "x is odd"
>>> }
>>> 
>>> you can write:
>>> 
>>> let message: String = if x % 2 == 0 { "x is even" } else { "x is odd" }
>>> 
>>> I'm not into any kind of compiler development, and don't know is it feasible to introduce such change at the moment, but at least, I would like to hear why not.
>>> Thanks!
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>  _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/e7773e40/attachment.html>


More information about the swift-evolution mailing list