Pattern Matching and Exhaustiveness

Expression or statement, exhaustive or not — PHP match surveyed across 10 languages on two dimensions.

C C23

C23
#include <stdio.h>

/* C has the classic switch statement: integer cases, manual break, no
   exhaustiveness check, and no value — returns happen inside the arms */
const char *label(int code) {
    switch (code) {
        case 200:
        case 201:
            return "success";
        case 404:
            return "not found";
        default:
            return "unknown";
    }
}

int main(void) {
    printf("%s\n", label(200));
    printf("%s\n", label(418));
    return 0;
}
Output
success
unknown

C++ C++26

C++26
#include <cstdio>

// C++ inherits C's switch statement; there is no match expression, so an
// if/else chain is the usual route once patterns go beyond integer equality
const char *label(int code) {
    if (code == 200 || code == 201) {
        return "success";
    } else if (code == 404) {
        return "not found";
    } else {
        return "unknown";
    }
}

int main() {
    std::printf("%s\n", label(200));
    std::printf("%s\n", label(418));
    return 0;
}
Output
success
unknown

C# v11.0.100

v11.0.100
string Label(int code) => code switch {
    // switch expression (C# 8+): an expression, no fall-through; _ is the
    // discard arm standing in for default — no compiler-enforced exhaustiveness
    200 or 201 => "success",
    404 => "not found",
    500 => "server error",
    _ => "unknown",
};

Console.WriteLine(Label(200));
Console.WriteLine(Label(404));
Console.WriteLine(Label(418));
Output
success
not found
unknown

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go has no match expression; switch is a statement with implicit break and
// no fall-through. Its specialty is the type switch over an interface.
func label(code int) string {
	switch code {
	case 200, 201:
		return "success"
	case 404:
		return "not found"
	default:
		return "unknown"
	}
}

func describe(v any) string {
	switch t := v.(type) { // type switch: the closest Go gets to pattern matching
	case int:
		return "int"
	case string:
		return "string"
	default:
		return fmt.Sprintf("other %T", t)
	}
}

func main() {
	fmt.Println(label(200))
	fmt.Println(describe(42))
	fmt.Println(describe(4.5))
}
Output
success
int
other float64

Java v27.0.1

v27.0.1
public class Main {
    enum Code { OK, NOT_FOUND, SERVER_ERROR }

    static String label(Code c) {
        // switch expression (Java 14+): arrow arms, no fall-through; switching
        // over an enum requires ALL constants or default — exhaustiveness is
        // compile-checked (Java 21+ pattern switch adds deconstruction)
        return switch (c) {
            case OK -> "success";
            case NOT_FOUND -> "not found";
            case SERVER_ERROR -> "server error";
        };
    }

    public static void main(String[] args) {
        System.out.println(label(Code.OK));
        System.out.println(label(Code.NOT_FOUND));
    }
}
Output
success
not found

JavaScript v26.13.1

v26.13.1
// JS has no match expression: only the switch statement — comparisons are
// strict (===), but it is a statement: no value, and fall-through to guard
function label(code) {
  switch (code) {
    case 200:
    case 201:
      return "success"; // grouped cases = the "OR" idiom, via fall-through
    case 404:
      return "not found";
    default:
      return "unknown";
  }
}

console.log(label(200));
console.log(label(418));
Output
success
unknown

PHP v8.6.0

v8.6.0
function label(int $code): string {
    // match is an expression: it produces a value directly, arms compare with ===
    return match ($code) {
        200, 201 => "success", // multiple conditions = logical OR
        404 => "not found",
        500 => "server error",
        default => "unknown",
    };
}

echo label(200), "\n";
echo label(404), "\n";
echo label(418), "\n";

// exhaustive check: without default, an unmatched subject throws UnhandledMatchError
try {
    match (302) {
        200 => "success",
    };
} catch (UnhandledMatchError $e) {
    echo "unhandled: ", $e->getMessage(), "\n";
}
Output
success
not found
unknown
unhandled: Unhandled match case 302

Python v3.15.2

v3.15.2
def label(code):
    # match-case (3.10+) is a STATEMENT: branches execute, no value produced;
    # exhaustiveness is NOT checked (a missed case just falls through)
    match code:
        case 200 | 201:
            return "success"
        case 404:
            return "not found"
        case 500:
            return "server error"
        case _:
            return "unknown"

print(label(200))
print(label(404))
print(label(418))
Output
success
not found
unknown

Rust v1.99.1

v1.99.1
enum Status {
    Ok,
    NotFound,
    ServerError,
}

fn label(s: Status) -> &'static str {
    // match is an expression and MUST be exhaustive: omitting a variant is a
    // compile error — the compiler, not the runtime, enforces the check
    match s {
        Status::Ok => "success",
        Status::NotFound => "not found",
        Status::ServerError => "server error",
    }
}

fn main() {
    println!("{}", label(Status::Ok));
    println!("{}", label(Status::NotFound));
    println!("{}", label(Status::ServerError));
}
Output
success
not found
server error

TypeScript v26.13.1

v26.13.1
// TS has no match expression either; the idiom is a discriminated union plus
// switch — with noImplicitReturns, a missed arm becomes a compile error
type Shape =
  | { kind: "circle"; r: number }
  | { kind: "rect"; w: number; h: number };

function area(s: Shape): number {
  switch (s.kind) {
    case "circle":
      return 3 * s.r * s.r; // 3 stands in for pi to keep the output stable
    case "rect":
      return s.w * s.h;
  }
}

console.log(area({ kind: "circle", r: 3 }));
console.log(area({ kind: "rect", w: 3, h: 4 }));
Output
27
12

Language differences

C: the classic switch statement (example): integer/char cases only, manual break, no exhaustiveness check, not an expression; -Wswitch gives a near-exhaustive nudge only for enum switches.

C++: the same switch as C; no match expression — an if/else chain is the usual route beyond integer equality (example); since C++17 if can carry an init-statement to tame scoping.

C#: the C# 8+ switch expression (example): an expression, no fall-through, or-combined conditions, _ discard as default; property/tuple/positional patterns supported, but a missing arm is not a compile error (it throws InvalidOperationException at runtime).

Go: no match expression; the switch statement breaks implicitly with no fall-through (example); the type switch is Go's closest construct to pattern matching; no compile-time exhaustiveness.

Java: the 14+ switch expression (example): arrow arms, no fall-through; over an enum it must cover every constant or have default — exhaustiveness is compile-checked; Java 21+ pattern switch adds deconstruction and guards.

JavaScript: no match expression, only the switch statement (example): strict === comparison, fall-through must be guarded; exhaustive checks are hand-rolled by throwing in default.

PHP: the 8.0+ match expression (example): strict === comparison, commas as logical OR, no fall-through, must be exhaustive or it throws UnhandledMatchError; the match(true) idiom covers range matching.

Python: the 3.10+ match-case is a statement (example): | combines, _ is the wildcard, sequences/mappings/classes can be destructured — the most capable, but not an expression and not exhaustiveness-checked.

Rust: the match grail (example): an expression with compile-enforced exhaustiveness plus binding/destructuring/guards; miss one variant and it fails to compile — nothing left to go wrong at runtime.

TypeScript: at the language level same as JS; the idiom is a discriminated union plus switch (example): noImplicitReturns, or a default narrowing to never, buys compile-time exhaustiveness.