Subsections

- Exercises

# Field selection

The field-selectors of a structure mode are used to “extract” the individual fields of a structure. For example, given this declaration for the structure `s`:

       STRUCT(INT i,CHAR c) s = (2,"e")

we can select the first field of `s` using the selection

       i OF s

The mode of the selection is `INT` and its value is `2`. Note that the construct `OF` is not an operator. The second field of `s` can be selected using the selection

       c OF s

whose mode is `CHAR` with value `"e"`. The field-selectors cannot be used on their own: they can only be used in a selection.

A selection can be used as an operand. Consider the formula

       i OF s * ABS c OF s

In the structure `method`, declared in the previous section, the procedure in the structure can be selected by

       p OF method

which has the mode `PROC(REAL)REAL`. For a reason which will be clarified in chapter 10, if you want to call this procedure, you must enclose the selection in parentheses:

       (p OF method)(0.5)

Remembering that the context of the actual-parameters of a procedure is strong, you could also write

       (p OF method)(int OF method)

where `int OF method` would be widened to a real number and the whole expression would yield a value of mode `REAL`.

The two fields of the structure `double` (also declared in the previous section), can be selected by writing

       c OF double
       s OF double

and their modes are `CHAR` and `STRUCT(INT i,j)` respectively. Now the fields of the inner structure `s` of `double` can be selected by writing

       i OF s OF double
       j OF s OF double

and both selections have mode `INT`.

Now consider the structure name `sn` declared by

       STRUCT(INT i,CHAR s) sn;

The mode of `sn` is

       REF STRUCT(INT i,CHAR s)

This means that the mode of the selection

       i OF sn

is not `INT`, but `REF INT`, and the mode of the selection

       c OF sn

is `REF CHAR`. That is, the modes of the fields of a structure name are all preceded by `REF` (they are all names). This is particularly important for recursively defined structures (see chapter 11). Thus, instead of assigning a complete structure using a structure-display, you can assign values to individual fields. That is, the assignment

       sn:=(3,"f")

is equivalent to the assignments

       i OF sn := 3;
       c OF sn := "f"

except that the assignments to the individual fields are separated by the go-on symbol (the semicolon `;`) and the two units in the structure-display are separated by a comma and so are elaborated collaterally.

Given the declaration and initial assignment

       STRUCT(CHAR c,STRUCT(INT i,j)s)dn:=double

the selection

       s OF dn

has the mode `REF STRUCT(INT i,j)`, and so you could assign directly to it:

       s OF dn:=(-1,-2)

as well as to one of its fields:

       j OF s OF dn:=0

## Exercises

7.3
  Given the declarations

         STRUCT(STRUCT(CHAR a,INT b)c,
                PROC(STRUCT(CHAR a,INT b))INT p,
                INT d)st;
         STRUCT(CHAR a,INT b)sta

  what is the mode of Ans
  (a)
    `c OF st`

  (b)
    `a OF c OF st`

  (c)
    `a OF sta`

  (d)
    `(p OF st)(sta)`

  (e)
    `b OF c OF st * b OF sta`

  (f)
    `sta:=c OF st`

7.4
  Declare a procedure which could be assigned to the selection `p OF st` in the last question. Ans