switch()

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Feb 16 08:29:09 PST 2014


On 2/16/14, Manu <turkeyman at gmail.com> wrote:
> case fall-through is not supported; explicit 'goto case n;' is required.

You can actually use "goto case;" now, without specifying the actual case.

> 'case 1, 3, 7, 8:' is awesome! ...but ranged cases have a totally different
> syntax: 'case 1: .. case 3:'
>
> Why settle on that syntax? The inconsistency looks kinda silly when they
> appear together in code.
> Surely it's possible to find a syntax that works without repeating case and
> ':'?

I've tried asking for it:
https://d.puremagic.com/issues/show_bug.cgi?id=11213

> 3.
> Why is 'default' necessary? If I'm not switching on an enumerated type,
> then many values are meaningless. requiring an empty 'default: break;' line
> at the end is annoying and noisy.

One reason is to allow runtime diagnostics or exceptions being thrown, e.g.:

switch (val)
{
    case "foo": ...
    case "bar": ...
    default: assert(0, format("Unhandled case: %s", val));
}


More information about the Digitalmars-d mailing list