#### 5.6.2 Functions and Variables for Structures

Global variable: structures

  `structures` is the list of user-defined structures defined by `defstruct`.

  ·

  Categories: Structures ·Global variables

Function: defstruct
`defstruct` (S(a_1, …, a_n))
`defstruct` (S(a_1 = v_1, …, a_n = v_n))

  Define a structure, which is a list of named fields a_1, …, a_n associated with a symbol S. An instance of a structure is just an expression which has operator S and exactly `n` arguments. `new(S)` creates a new instance of structure S.

  An argument which is just a symbol a specifies the name of a field. An argument which is an equation `a = v` specifies the field name a and its default value v. The default value can be any expression.

  `defstruct` puts S on the list of user-defined structures, `structures`.

  `kill(S)` removes S from the list of user-defined structures, and removes the structure definition.

  Examples:

      (%i1) defstruct (foo (a, b, c));
      (%o1)                    [foo(a, b, c)]
      (%i2) structures;
      (%o2)                    [foo(a, b, c)]
      (%i3) new (foo);
      (%o3)                     foo(a, b, c)
      (%i4) defstruct (bar (v, w, x = 123, y = %pi));
      (%o4)             [bar(v, w, x = 123, y = %pi)]
      (%i5) structures;
      (%o5)      [foo(a, b, c), bar(v, w, x = 123, y = %pi)]
      (%i6) new (bar);
      (%o6)              bar(v, w, x = 123, y = %pi)
      (%i7) kill (foo);
      (%o7)                         done
      (%i8) structures;
      (%o8)             [bar(v, w, x = 123, y = %pi)]

  ·

  Categories: Structures

Function: new
`new` (S)
`new` (S (v_1, …, v_n))

  `new` creates new instances of structures.

  `new(S)` creates a new instance of structure S in which each field is assigned its default value, if any, or no value at all if no default was specified in the structure definition.

  `new(S(v_1, ..., v_n))` creates a new instance of S in which fields are assigned the values v_1, …, v_n.

  Examples:

      (%i1) defstruct (foo (w, x = %e, y = 42, z));
      (%o1)              [foo(w, x = %e, y = 42, z)]
      (%i2) new (foo);
      (%o2)               foo(w, x = %e, y = 42, z)
      (%i3) new (foo (1, 2, 4, 8));
      (%o3)            foo(w = 1, x = 2, y = 4, z = 8)

  ·

  Categories: Structures

Operator: @

  `@` is the structure field access operator. The expression `x@ a` refers to the value of field a of the structure instance x. The field name is not evaluated.

  If the field a in x has not been assigned a value, `x@ a` evaluates to itself.

  `kill(x@ a)` removes the value of field a in x.

  Examples:

      (%i1) defstruct (foo (x, y, z));
      (%o1)                    [foo(x, y, z)]
      (%i2) u : new (foo (123, a - b, %pi));
      (%o2)           foo(x = 123, y = a - b, z = %pi)
      (%i3) u@z;
      (%o3)                          %pi
      (%i4) u@z : %e;
      (%o4)                          %e
      (%i5) u;
      (%o5)            foo(x = 123, y = a - b, z = %e)
      (%i6) kill (u@z);
      (%o6)                         done
      (%i7) u;
      (%o7)              foo(x = 123, y = a - b, z)
      (%i8) u@z;
      (%o8)                          u@z

  The field name is not evaluated.

      (%i1) defstruct (bar (g, h));
      (%o1)                      [bar(g, h)]
      (%i2) x : new (bar);
      (%o2)                       bar(g, h)
      (%i3) x@h : 42;
      (%o3)                          42
      (%i4) h : 123;
      (%o4)                          123
      (%i5) x@h;
      (%o5)                          42
      (%i6) x@h : 19;
      (%o6)                          19
      (%i7) x;
      (%o7)                    bar(g, h = 19)
      (%i8) h;
      (%o8)                          123

  ·

  Categories: Structures ·Operators