PreviousUpNext

10.5.6  Record Expressions

Mythryl records are like tuples except that fields are accessed by name rather than by number. Records are in fact just syntactic sugar for tuples — the compiler converts records into tuples early in processing after which they are compiled identically. Record labels occupy a separate namespace. Syntactically, records are created using curly braces rather than parentheses:

    linux$ my

    eval:  r = { foo => 1, bar => 2.0, zot => "three" };        # Construct a record.

    eval:  r.foo;                                               # Access field 'foo'

    1

    eval:  r.bar;                                               # Access field 'bar'

    2.0

    eval:  r.zot;                                               # Access field 'zot'

    "three"

    eval:  .foo r;                                              # Access field 'foo', alternate syntax.

    1

    eval:  .bar r;                                              # Access field 'bar', alternate syntax.

    2.0

    eval:  .zot r;                                              # Access field 'zot', alternate syntax.

    "three"

As with tuples, record fields are in practice usually accessed via pattern-matching:

    linux$ my

    eval:  r = { foo => 1, bar => 2.0, zot => "three" };        # Construct a record.

    eval:  my { foo => f, bar => b, zot => z } = r;             # Unpack it into f,b,z via pattern-matching.

    eval:  f;

    1

    eval:  b;

    2.0

    eval:  z;

    "three"

Frequently a record is unpacked into variables with the same names as the record fields:

    linux$ my

    eval:  r = { foo => 1, bar => 2.0, zot => "three" };        # Construct a record.

    eval:  my { foo => foo, bar => bar, zot => zot } = r;       # Unpack it into foo, bar, zot via pattern-matching.

    eval:  foo;

    1

    eval:  bar;

    2.0

    eval:  zot;

    "three"

Mythryl allows this case to be specially abbreviated:

    linux$ my

    eval:  r = { foo => 1, bar => 2.0, zot => "three" };        # Construct a record.

    eval:  my { foo, bar, zot } = r;                            # Unpack it into foo, bar, zot via pattern-matching.

    eval:  foo;

    1

    eval:  bar;

    2.0

    eval:  zot;

    "three"

A similar abbreviation is supported when creating a record:

    linux$ my

    eval:  foo = 1;                             # Name an integer value.

    1

    eval:  bar = 2.0;                           # Name a float value.

    2.0

    eval:  zot = "three";                       # Name a string value.

    "three"

    eval:  r = { foo, bar, zot };               # Abbreviated record creation syntax.

    eval:  r.foo;                               # Extract field 'foo' from record.

    1

    eval:  r.bar;                               # Extract field 'bar' from record.

    2.0

    eval:  r.zot;                               # Extract field 'zot' from record.

    "three"

Mythryl records are exactly as cheap as Mythryl tuples, and as with tuples, it is common and encouraged for Mythryl programs to create and discard millions of records during a run.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext