Skip to content

When

Description

An arm of a Match, in one of two forms (selected by argument count):

  • When(T) – static: runs iff no earlier arm matched and the

subject’s static type is T; binds it to the value.

  • When(N, T) – variant: runs iff no earlier arm matched and the

subject’s runtime tag is Variant(N, ...)’s T; binds it to the T-typed payload.

Success

Body runs at most once, only on a match, with it bound.

Failure

Macro cannot fail. Usable only inside a Match. When(N, T) for a T the variant cannot hold is a compile error (no as_T member).

Usage example (Cross-references)

Usage examples (Cross-references)
        bool   hit = false;
        Match(n) {
            When(Number, int) hit    = (it == 42);
            When(Number, double) hit = false;
        }
        Match(n) {
            When(Number, int) hit    = (it == 42);
            When(Number, double) hit = false;
        }
        bool   hd = false;
        Match(m) {
            When(Number, int) hd    = false;
            When(Number, double) hd = (it == 2.5);
        }
        Match(m) {
            When(Number, int) hd    = false;
            When(Number, double) hd = (it == 2.5);
        }
        return hit && hd;
        double bd = 0.0;
        Match(a) {
            When(Number, int) ai    = it * 2; // it : int
            When(Number, double) bd = it;
        }
        Match(a) {
            When(Number, int) ai    = it * 2; // it : int
            When(Number, double) bd = it;
        }
        Match(b) {
        }
        Match(b) {
            When(Number, int) ai    = it;
            When(Number, double) bd = it * 2.0; // it : double
        }
        Match(b) {
            When(Number, int) ai    = it;
            When(Number, double) bd = it * 2.0; // it : double
        }
        return ai == 20 && bd == 3.0;
    static Number twice(Number n) {
        Match(n) {
            When(Number, int) return Number_from_int(it * 2);
            When(Number, double) return Number_from_double(it * 2.0);
        }
        Match(n) {
            When(Number, int) return Number_from_int(it * 2);
            When(Number, double) return Number_from_double(it * 2.0);
        }
        return n;
        bool   ok = false;
        Match(r) {
            When(Number, int) ok    = (it == 42);
            When(Number, double) ok = false;
        }
        Match(r) {
            When(Number, int) ok    = (it == 42);
            When(Number, double) ok = false;
        }
        return ok;
        // Match directly on a function-return rvalue; float-less Number -> Otherwise unused.
        Match(twice(Number_from_double(1.5))) {
            When(Number, int) ok    = false;
            When(Number, double) ok = (it == 3.0);
            Otherwise ok            = false;
        Match(twice(Number_from_double(1.5))) {
            When(Number, int) ok    = false;
            When(Number, double) ok = (it == 3.0);
            Otherwise ok            = false;
        }
        bool ok = false;
        Match(c) {
            When(Cell, int) ok  = false;
            When(Cell, char) ok = (it == 'Z');
        }
        Match(c) {
            When(Cell, int) ok  = false;
            When(Cell, char) ok = (it == 'Z');
        }
        return ok;
        for (int k = 0; k < 3; k++) {
            Match(vals[k]) {
                When(Tri, int) ok = ok && (it == 7), seen |= 1;
                When(Tri, float) ok = ok && (it == 1.5f), seen |= 2;
                When(Tri, char) ok = ok && (it == 'A'), seen |= 4;
            Match(vals[k]) {
                When(Tri, int) ok = ok && (it == 7), seen |= 1;
                When(Tri, float) ok = ok && (it == 1.5f), seen |= 2;
                When(Tri, char) ok = ok && (it == 'A'), seen |= 4;
            }
                When(Tri, int) ok = ok && (it == 7), seen |= 1;
                When(Tri, float) ok = ok && (it == 1.5f), seen |= 2;
                When(Tri, char) ok = ok && (it == 'A'), seen |= 4;
            }
        }
        int    count = 0;
        Match(n) {
            When(Number, int) count++;
            When(Number, double) count++;
            Otherwise count++;
        Match(n) {
            When(Number, int) count++;
            When(Number, double) count++;
            Otherwise count++;
        }
        Number n = Number_from_double(9.0); // holds double, only int handled, no Otherwise
        Match(n) {
            When(Number, int)(void) it;
        }
        return true; // unreachable -- the match aborts first
        int tag = -1;
        Match(i) {
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        Match(i) {
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        }
    
        Match(d) {
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        Match(d) {
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        }
        // Unlisted type falls to Otherwise.
        Match(p) {
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        Match(p) {
            When(int) tag    = 0;
            When(double) tag = 1;
            Otherwise tag    = 9;
        }
        double rd = 0.0;
        Match(i) {
            When(int) ri    = it * 2;
            When(double) rd = it;
        }
        Match(i) {
            When(int) ri    = it * 2;
            When(double) rd = it;
        }
        Match(d) {
        }
        Match(d) {
            When(int) ri    = it;
            When(double) rd = it * 2.0;
        }
        Match(d) {
            When(int) ri    = it;
            When(double) rd = it * 2.0;
        }
        return ri == 42 && rd == 3.0;
        // Scrutinee is an rvalue; copied by value into `it`.
        Match(i + 1) {
            When(int) hit    = (it == 5);
            When(double) hit = false;
        }
        Match(i + 1) {
            When(int) hit    = (it == 5);
            When(double) hit = false;
        }
        return hit;
        int count = 0;
        Match(i) {
            When(int) count++;
            When(double) count++;
            Otherwise count++;
        Match(i) {
            When(int) count++;
            When(double) count++;
            Otherwise count++;
        }
        bool       hit = false;
        Match(p) {
            When(int) hit    = false;
            When(double) hit = false;
            Otherwise hit    = true;
        Match(p) {
            When(int) hit    = false;
            When(double) hit = false;
            Otherwise hit    = true;
        }
        double id = 0.0;
        Match(i) {
            When(int) {
                Match(d) {
                    When(int) id    = -1.0;
            When(int) {
                Match(d) {
                    When(int) id    = -1.0;
                    When(double) id = it; // inner `it` : double
                }
                Match(d) {
                    When(int) id    = -1.0;
                    When(double) id = it; // inner `it` : double
                }
                oi = it;                  // back to outer `it` : int
                oi = it;                  // back to outer `it` : int
            }
            When(double) oi = -1;
        }
        return oi == 10 && id == 3.5;
        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
Last updated on