if case


If-Case Statement

An if-case statement is a compact form of checking a single value against multiple possible constant values. It is analogous to a switch statement in C or C++, but each case does not fall through by default.

if value == {
    case constant_a; /* statements */
    case constant_b; /* statements */
    case constant_c; /* statements */
    case;            /* default statements */
}

When value matches the constant after a case, that case's block of statements is executed, and then control flow jumps past the end of the if. Each case is its own scope, so local variables declared in one case do not conflict with those in another. The bare case; at the end (with no value) is the default: it executes when no other case matches.

Each case expression must be a compile-time constant. Runtime variables and function calls are not allowed. A given value may not appear in more than one case.

Any type that is representable as a constant and comparable via the built-in == operator can be used, including enums, string, Type, and function pointers. Structs are not supported because the language does not provide a built-in == for structs, and allowing user-defined overloads would prevent the compiler from optimizing and validating the case table.

#through

By default, execution of an if-case statement does not fall through from one case to the next. To opt into C-like fall-through behavior, place #through as the last statement in a case block. Execution will then continue into the next case's block.

if value == {
    case constant_a; #through;
    case constant_b; #through;
    case constant_c; /* statements executed for a, b, or c */

    case constant_d; #through;
    case;            /* statements executed for d or any unmatched value */
}

A common use of #through is to group several case values so they all execute the same block of statements.

#complete

Marking an if-case statement with #complete instructs the compiler to verify that every possible value of the enum type appears in a case. If any value is missing, the program fails to compile. This directive only applies to enum types.

if #complete value == {
    case constant_a; /* statements */
    case constant_b; /* statements */
    case constant_c; /* statements */
    case;            /* default statements */
}

This is useful in large programs where adding a new enum value should force all related if-case statements to be updated. A bare case; (default) is still permitted and handles values that are technically possible at runtime due to memory corruption or other bugs, even when all enum members are covered.