Skip to content

Otherwise

Description

The fallback arm of a Match. Runs iff no earlier arm matched.

Success

Body runs at most once, only when every preceding arm missed.

Failure

Macro cannot fail. Usable only inside a Match.

Usage example (Cross-references)

Usage examples (Cross-references)
            When(Number, int) ok    = false;
            When(Number, double) ok = (it == 3.0);
            Otherwise ok            = false;
        }
        return ok;
            When(Number, int) count++;
            When(Number, double) count++;
            Otherwise count++;
        }
        return count == 1;
    
    bool deadend_variant_nonexhaustive(void) {
        Number n = Number_from_double(9.0); // holds double, only int handled, no Otherwise
        Match(n) {
            When(Number, int)(void) it;
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        }
        bool ok = tag == 0;
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        }
        ok = ok && tag == 1;
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        }
        ok = ok && tag == 9;
            When(int) count++;
            When(double) count++;
            Otherwise count++;
        }
        return count == 1;
            When(int) hit    = false;
            When(double) hit = false;
            Otherwise hit    = true;
        }
        return hit;
        Position2D p = {1.0f, 2.0f};
        Match(p) {
            When(int)(void) it; // never matches Position2D, and there is no Otherwise
        }
        return true;            // unreachable -- the match aborts first
    #define Match(x)                                                                                                       \
        for (bool MisraMatched = false, UNPL(tm_once) = true; UNPL(tm_once); UNPL(tm_once) = false,                        \
                  ASSERT_OR_FATAL(MisraMatched, "Match: no arm matched and no Otherwise (non-exhaustive)"))                \
            for (TYPE_OF(x) MisraSubject = (x), *UNPL(tm_loop) = &MisraSubject; UNPL(tm_loop); UNPL(tm_loop) = NULL)
Last updated on