[swift-evolution] [Pitch] Introduce continue to switch statements

Leonardo Pessoa me at lmpessoa.com
Mon Jul 11 10:21:00 CDT 2016


I'll have to give this a -1. Code seems to be confusing with this new
use of continue and we may easily lose control of the flow. If we are
to allow another case to be executed, we should be able to explicitly
direct the flow to the case we want. C# does that using the goto
keyword (yes, I know). I would support this (goto) here but it however
was designed to work well with constant cases and I don't see how this
could work with cases with where expressions like the ones in this
example.

L


On 11 July 2016 at 11:54, Erica Sadun via swift-evolution
<swift-evolution at swift.org> wrote:
>
> On Jul 11, 2016, at 4:49 AM, Ross O'Brien <narrativium+swift at gmail.com>
> wrote:
>
> I'm in favour of this concept being available in Swift, but it does need to
> make a clear distinction somewhere between a case which matches anything
> (and can catch any fallthrough/continue) and a case which matches none of
> the above. I don't know whether we need to introduce an explicit term for
> this, or whether we make this the distinction between 'case _:' and
> 'default:' (with the side-effect that we can now justify 'default' in the
> language).
>
> For example: let's use this switch to play fizzbuzz.
>
> switch value
> {
>   case x where x % 3 == 0:
>     print("fizz")
>     continue
>   case x where x % 5 == 0:
>     print("buzz")
>   default:
>     print(value)
> }
>
> I know this is a trivial example, but it's also a frequent example used to
> teach switch to programmers.
> The keywords in play are 'continue' and 'default'. A multiple of 5 but not 3
> will print 'buzz' and be done. A multiple of 3 and 5 will print 'fizz buzz'.
> Any number not a multiple of 3 or 5 will say its value. Crucially, a
> multiple of 3 but not 5 will say fizz, won't say buzz, and won't say the
> number itself - whereas if we used 'case _', it would say fizz and then the
> number itself.
>
>
>
> The standard question asks you to print the value and then fizz and/or buzz
> on the same line and then move to the next line.
>
> If you use the rules you stated ("Any number not a multiple of 3 or 5 will
> say its value."), it would look like this:
>
> switch value
> {
> case _ where !(x % 5 == 0 || x % 3 == 0):
>     print(value)
> case x where x % 3 == 0:
>     print("fizz", terminator: "")
>     continue
> case x where x % 5 == 0:
>     print("buzz", terminator: "")
>     continue
> default:
>     print("")
>     break
> }
>
> If you use the standard rules, it looks like this:
>
> switch value
> {
> case _:
>     print(value, terminator: "")
> case x where x % 3 == 0:
>     print(" fizz", terminator: "")
>     continue
> case x where x % 5 == 0:
>     print(" buzz", terminator: "")
>     continue
> default:
>     print("")
>     break
> }
>
> -- E
>
>
> _______________________________________________
> 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