[Swift-devel] the switch statement

Tim Armstrong tim.g.armstrong at gmail.com
Fri May 29 19:26:25 CDT 2015


How about keep it as is but if two cases have the same constant value generate an error/warning? I feel like if you're using non-constant expressions in case labels the programmer is responsible for dealing with overlapping cases.

It seems sensible to me to run the first matching case or default if none match. Running multiple cases seems counter intuitive.

 Swift/t doesn't support expressions in switch anyway if that matters.

-----Original Message-----
From: "Mihael Hategan" <hategan at mcs.anl.gov>
Sent: ‎5/‎29/‎2015 6:46 PM
To: "Swift Devel" <swift-devel at ci.uchicago.edu>
Subject: [Swift-devel] the switch statement

So I have some bad news. The switch statement is broken.

In most c-like languages, the case expressions are restricted to
literals. The reason is that it is the only way to reliably check for
conflicting cases. Allowing arbitrary expressions means that you can't
assert that the cases are non-overlapping at compile-time.

Swift allows expressions in cases. Furthermore, it implements the cases
as 

if (expr == case1expr) {statements1}
else if (expr == case2expr) {statements2}
...

This means that when two cases have the same value, the second one will
be quietly ignored.

We have two options I think:

1. Remove the else. Run statements in all cases that have the right
value
2. Restrict case expressions to literals and check that there are no two
cases with the same value at compile-time.

Thoughts?

Mihael

_______________________________________________
Swift-devel mailing list
Swift-devel at ci.uchicago.edu
https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/swift-devel/attachments/20150529/338497ab/attachment.html>


More information about the Swift-devel mailing list