> On Jul 11, 2016, at 4:49 AM, Ross O'Brien <[email protected]> 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
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution