Skip to content

Shorthand for repeatedly using the same combination of values in a switch statement, eg writing compilers #3112

@joshring

Description

@joshring

From the stream

Image

Given we have an enum

enum Foo 
{
    ABC,
    DEF,
    GGG,
    HHH
}

If we need to write this combination of cases in many places at once in the code, eg a compiler or any other state machine

switch(f)
{
    case ABC:
    case GGG:
        io::printn("handle case");
    case DEF:
    case HHH:
        io::print("handle case2");
}

This could be written, helpful if you're writing this in many places throughout your code, also can be a bit more self documenting

const Foo[2] EXPR = {ABC, GGG};
const Foo[2] LITERAL = {DEF, HHH};

switch(f)
{
    case EXPR:
        io::printn("handle case");
    case LITERAL:
        io::printn("handle case");
}

I think that also allows this by extension, but not sure

switch(f)
{
    case {ABC, GGG}:
        io::printn("handle case");
    case {DEF, HHH}:
        io::printn("handle case");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions