[swift-evolution] [Idea] Wrap switch cases in curly braces
Jon Akhtar
jona at herbalife.com
Thu Jul 7 16:09:14 CDT 2016
You can have this now:
switch x {
case 0: { print(0) }()
case 1: { print(1) }()
case 2: { print(2) }()
default: { print("other²) }()
}
However it isn¹t quite the same.
Case with a colon has far too many parallels in other very common
languages IMHO. I am not sure being consistent is always a must, it should
add something, ease of understanding, readability, etc. I don¹t think
braces here really do. Like I mentioned though, you can have them now if
you don¹t mind the extra parens.
Cheers
On 7/7/16, 13:07, "swift-evolution-bounces at swift.org on behalf of G B via
swift-evolution" <swift-evolution-bounces at swift.org on behalf of
swift-evolution at swift.org> wrote:
>It has always seemed odd to me that `case`s use a colon as a delimiter
>rather than curly braces like everything else. Is there a reason for
>this other than the legacy of C-like languages?
>
>If I wanted to write a series of branching `if` \ `else` statements I
>would do it like so:
>
>if x==0 { print(0) }
>else if x==1 { print (1) }
>else if x==2 { print(2) }
>else { print("other²) }
>
>I believe all flow control is wrapped in curly braces, except for `case`s
>inside a `switch`:
>
>switch x {
>case 0: print(0)
>case 1: print(1)
>case 2: print(2)
>default: print("other")
>}
>
>
>I feel like this would be more consistent with the rest of the syntax:
>
>switch x {
>case 0 { print(0) }
>case 1 { print(1) }
>case 2 { print(2) }
>default { print("other²) }
>}
>
>The colon syntax evokes a label, but the modern, complex `case`
>statements in Swift don¹t act much like labels.
>_______________________________________________
>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