[swift-evolution] Pitch: disallow `()` from Switch statement case satisfaction

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Thu Oct 13 10:19:59 CDT 2016


If I might be so bold, perhaps we should consider the opposite. Suppose you
have a conditional statement inside a loop. It would be easier for the
reader to understand what it does if “break” meant the same thing
regardless of whether you used “if” or “switch” for the condition.

Right now, these two loops behave differently:

let seq = [1, 2, 3]

for x in seq {  // prints ! 2 ! 3 !
    switch x {
    case 1: break
    case _: print(x)
    }
    print("!")
}

for x in seq {  // prints nothing
    if x == 1 {
        break
    } else {
        print(x)
    }
    print("!")
}

In particular, the current behavior of “break” means you need to label the
loop if you want to break out of it from a “switch”, but not from an “if”.
This is at least inconsistent.

Nevin



On Thu, Oct 13, 2016 at 7:59 AM, Haravikk via swift-evolution <
swift-evolution at swift.org> wrote:

>
> On 13 Oct 2016, at 12:26, Alex Blewitt <alblue at apple.com> wrote:
>
> On 13 Oct 2016, at 11:06, Haravikk via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>
>
> On 11 Oct 2016, at 19:43, Erica Sadun via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> I thought this was long gone but today I found out it is still legal:
>
> switch i {
> case 4 ... 6: ()
> case 3: print("Here")
> default: break
> }
>
> Is there a motivating factor for keeping this in the language? The
> compiler picks up on Void and emits an error. You'd think () would produce
> the same results but it doesn't.
>
> — Erica
>
>
> Hopefully I'm not the only one but… how are we *supposed* to be doing
> this? Because () is exactly what I've been using the entire time for cases
> that I want to ignore (or are handled in code outside the switch). I'm
> going to have a few dozen files to edit if there's something else I'm
> supposed to be using…
>
>
> You can have a 'break' there, which is equivalent to a nop but without a
> return value. Whether that's what you're supposed to do or not is a
> different issue :)
>
>
> Perhaps too philosophical a question? ^^
>
> You're right though, I probably should be using breaks as they're more
> explicit, and I suppose fallthrough should work too so all cases should be
> covered without having to use (), in that case it maybe is something worth
> getting rid of?
>
> _______________________________________________
> 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/20161013/1633282d/attachment.html>


More information about the swift-evolution mailing list