next up previous contents
Next: Resolving ambiguities between array Up: Vectors and vector operators Previous: Vectors as conditions   Contents


Coercion of structures to vectors

A reference to a variable of structured type is converted to a vector of its components. The components of the structure appear in this vector in reverse lexicographic order according to the component name. That is, the lexically least component appears in the least significant position of the vector. For example, if x is declared in this way:

        x : struct {
          green, red, blue : boolean;
        }

then the expression ``x'' is equivalent to

       [x.red,x.green,x.blue]
This expansion makes it possible, for example, to compare two structures for equality, or to assign one structure to another. For example, if we have declared:
        x,y : struct {
          red, greed, blue : boolean;
        }
Then the expression x=y is equivalent to
        x.blue = y.blue & x.green = y.green & x.red = y.red
Similarly, the assignment
        x := y;
is equivalent to
        [x.blue,x.green,x.red] := [y.blue,y.green,y.red];
which is equivalent to
        x.blue := y.blue;
        x.green := y.green;
        x.red := y.red;

Warning! If you assign a record to a record of a different type, you will probably not get the result you want, and no type error will be generated.

Note that the components of structures need not be declared using the struct keyword in order for the structure to be expanded in this way. The same result would be obtained, for example, by declaring x as follows:

        x.red : boolean;
        x.green : boolean;
        x.blue : boolean;


next up previous contents
Next: Resolving ambiguities between array Up: Vectors and vector operators Previous: Vectors as conditions   Contents
2002-10-28