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)
    #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)
            When(Number, int, v) ok    = false;
            When(Number, double, v) ok = (v == 3.0);
            Otherwise ok               = false;
        }
        return ok;
            When(Number, int, v) count++;
            When(Number, double, v) count++;
            Otherwise count++;
        }
        return count == 1;
    // Deadend: a non-exhaustive variant match aborts rather than fall through.
    bool deadend_variant_nonexhaustive(void) {
        Number n = Number_from_double(9.0); // holds double, only int handled, no Otherwise
        Match(n) {
            When(Number, int, v)(void) v;
            When(int, v) tag    = 0;
            When(double, v) tag = 1;
            Otherwise tag       = 9;
        }
        bool ok = tag == 0;
            When(int, v) tag    = 0;
            When(double, v) tag = 1;
            Otherwise tag       = 9;
        }
        ok = ok && tag == 1;
            When(int, v) tag    = 0;
            When(double, v) tag = 1;
            Otherwise tag       = 9;
        }
        ok = ok && tag == 9;
            When(int, v) count++;
            When(double, v) count++;
            Otherwise count++;
        }
        return count == 1;
            When(int, v) hit    = false;
            When(double, v) hit = false;
            Otherwise hit       = true;
        }
        return hit;
        Position2D p = {1.0f, 2.0f};
        Match(p) {
            When(int, v)(void) v; // never matches Position2D, and there is no Otherwise
        }
        return true;              // unreachable -- the match aborts first
Last updated on