Structure Expressions

Structure Literals

A structure can be written directly into an expression using the form :-

structlit ::= { [ fieldexpr [[ , fieldexpr ]] ] }

fieldexpr ::= [ fieldname -> ] expr

This syntax is essentially the same as for the actual parameters of a function. If a field name is not specified the the next field in turn is assigned the result of the expression. If the field name is specified that that field is assigned. If a field is not specified then the default expression is used - this expression will be evaluated at runtime each time the structure is assigned.

This mechanism will initialise all the field in the structure, including fields flagged as constant.

If a field is omitted but there is no default expression then a compile time error is generated.

It should be noted that this syntax does not specify which particular structure type is being used, so this can only be used where the system is expecting a particular type, such as when being assigned to a datum or the parameter of a function.

Field Access Operator

The field access operator is used to access a specific field within a structure datum. The operator is a single '.' ( dot ). The value to the left must be a datum containing the structure and the right must be the name of a field within that structure. This field name is fixed at compile time - you cannot change fields at runtime.

Type MyRecType = { Number fld1, Number fld2 = 2, Number fld3 };
 . . . 
MyRecType rec;
 . . .
rec := { 10, fld3 -> 30 };

If you try to assign a value to a field with this syntax for a field that is flagged as constant, the compiler will generate an error.

Structure Modification Operator

The structure modification operator ( sometimes call the brace operator ) has the following syntax :-

structmod ::= srructexpr structlit

The syntax for a structure modification expression is like the syntax for a structure literal but with a base structure prepended. The base structure is an expression that yields a structure of known type ( so it cannot itself be a structure literal ). This base literal will act as the source off all the values that are not specified on the right-hand side - thus it is generating a modified version of the base structure. As this is used to set a compete new structure rather than modifying the base structure itself the Const restrictions do not apply.

MyRecType rec, newrec;
 . . . 
rec := { 10, fld3 -> 30 };
newrec := rec { 200, fld3 -> 300 };

This mechanism is the only way to create graphics context objects ( apart for the origin ).

Equality Operators

The two equality operators, '==' and '<>', will work on structure operands. The two structures are compared field by field. The left side must be an expression with a deducible type ( i.e. not a literal structure ).