45.1 Accessing Record Elements

rec.name

The above construct evaluates to the value of the record component with the name name in the record rec. Note that the name is not evaluated, i.e., it is taken literal.

    gap> r := rec( a := 1, b := 2 );;
    gap> r.a;
    1
    gap> r.b;
    2 

rec.(name)

This construct is similar to the above construct. The difference is that the second operand name is evaluated. It must evaluate to a string or an integer otherwise an error is signalled. The construct then evaluates to the element of the record rec whose name is, as a string, equal to name.

    gap> old := rec( a := 1, b := 2 );;
    gap> new := rec();
    rec(
       )
    gap> for i  in RecFields( old )  do
    >        new.(i) := old.(i);
    >    od;
    gap> new;
    rec(
      a := 1,
      b := 2 ) 

If rec does not evaluate to a record, or if name does not evaluate to a string, or if rec.name is unbound, an error is signalled. As usual you can leave the break loop (see Break Loops) with quit;. On the other hand you can return a result to be used in place of the record element by return expr;.

Up Top Next
Index

GAP 3.4.4
April 1997