45.7 Printing of Records

Print( rec )

If a record is printed by Print (see Print, PrintTo, and AppendTo) or by the main loop (see Main Loop), it is usually printed as record literal, i.e., as a sequence of components, each in the format name := value, separated by commas and enclosed in rec( and ).

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

But if the record has an element with the name operations that is a record, and if this record has an element with the name Print that is a function, then this function is called with the record as argument. This function must print whatever the printed representation of the record should look like.

The following example shows one piece of the definition of residue classes, using record operations. Of course this is far from a complete implementation (see About Defining New Group Elements). Note that it is typical for records that mimic group elements to print as a function call that, when evaluated, will create this group element record.

    gap> ResidueClassOps := rec( );;
    gap> ResidueClassOps.Print := function ( r )
    >   Print( "ResidueClass( ",
    >               r.representative mod r.modulus, ", ",
    >               r.modulus, " )" );
    > end;;
    gap> ResidueClass := function ( representative, modulus )
    >   return rec(
    >     representative := representative,
    >     modulus        := modulus,
    >     operations     := ResidueClassOps );
    > end;;
    gap> l := ResidueClass( 33, 23 );
    ResidueClass( 10, 23 ) 

Previous Up Top Next
Index

GAP 3.4.4
April 1997