21.6.19 Struct Pretty Printers

By default the printing statements will print a very detailed written representation of struct and union values. This implies printing out every detail of every field.

However, it is possible to change the written representation of a struct value by defining pretty-printing methods, which have prototypes:

method _print = void: { method_body }

If a struct or union type has a method called _print, then it is invoked every time a printed representation of the value shall be printed on the terminal.

For example a value of a struct type:

type Switch =
  struct
  {
    int<32> state;

    method _print = void:
    {
      printf "#<switch:%i32d>", state;
    }
  };

When printed when pretty-printers are active, will result in:

(poke) .set pretty-print yes
(poke) Switch { state = 1 }
#<switch:1>

Struct or union fields can also have pretty-printers defined for them: just define a method called _print_field_name to pretty-print a particular field.

For example a value of a struct type:

type Switch =
  struct
  {
    int<32> state;

    method _print_state = void:
    {
      printf ("#<%s>", state ? "on" : "off");
    }
  };

This will result in:

(poke) .set pretty-print yes
(poke) Switch { state = 1 }
Switch {
  state=#<on>
}