match

A match statement is similar to a switch statement in C-like languages.

A case body breaks automatically, unless it ends with a fallthrough statement (not like C).

import "fmt.odin";

main :: proc() {
    fmt.print("The current operating system is ");
    match os := ODIN_OS; os {
    case "windows":
        fmt.println("Windows.");
    case "linux":
        fmt.println("Linux.");
    default:
        fmt.println("who knows?");
    }
}

A match cases are evaluated from top to bottom, stopping when the case true.

match i {
case 0:
case foo():
}

foo is not called if i==0.

A match without a condition is the same as if the condition was true. This can be used a cleaner long if-then-else chain with the ability to break and fallthrough.

t := (12 * 42) % 7;
match {
case t < 2:
    fmt.println("t<2");
case t < 6: 
    fmt.println("t<6");
    fallthrough;
default:
    fmt.println("Whoop!");
}

match in

match in

TODO:

results matching ""

    No results matching ""