[swift-evolution] Control Flow Expressions

Sergey Shulepov s.pepyakin at gmail.com
Fri Dec 4 05:39:00 CST 2015


Agree on the if-as-an-expression part, that is possible, but it can be cumbersome if branches contain more than few trivial expressions. 
Whenever such situations occur you often have to break your expressions to separate methods just to split things up.

As for switch-as-an-expression, I'm not sure I got it right, can you elaborate on this?

> On 04 Dec 2015, at 14:07, Maxthon Chan <xcvista at me.com> wrote:
> 
> I believe switch-as-an-expression can be implemented as a dictionary lookup, and if-as-an-expression is the ternary operator ?:
> 
>> On Dec 4, 2015, at 19:05, Sergey Shulepov <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
> 



More information about the swift-evolution mailing list