PreviousUpNext

10.8.5  Record Patterns

Record patterns are written using syntax essentially identical to those of record expressions: A comma-separated list of pattern elements wrapped in curly brackets where each element consists of a name => value pair:

    linux$ cat my-script
    #!/usr/bin/mythryl

    r = { name => "Kim", age => 17 };  # Record expression.

    case r
        { name => n, age => i }   # Record pattern.
            =>
            printf "%s is %d.\n" n i;
    esac;

    linux$ ./my-script
    Kim is 17.

Frequently record fields are pattern-matched into variables with the same names:

    #!/usr/bin/mythryl

    r = { name => "Kim", age => 17 };

    case r
        { name => name, age => age }
            =>
            printf "%s is %d.\n" name age;
    esac;

    linux$ ./my-script
    Kim is 17.

In this case a special abbreviation is supported:

    linux$ cat my-script
    #!/usr/bin/mythryl

    r = { name => "Kim", age => 17 };

    case r
        { name, age }
            =>
            printf "%s is %d.\n" name age;
    esac;

    linux$ ./my-script
    Kim is 17.

Record patterns may be nested arbitrarily with each other and with other types of patterns:

    linux$ cat my-script
    #!/usr/bin/mythryl

    r = { name => "Kim", coordinate => (1121, 592) };

    case r
        { name, coordinate => (i,j) }
            =>
            printf "%s is at (%d,%d).\n" name i j;
    esac;

    linux$ ./my-script
    Kim is at (1121,592).

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext