next up previous contents index
Next: Copy On Write Up: ValuesTypes, and Constants Previous: References

Constant Values

 

 

The const keyword is used to indicate that certain values cannot be modified. In general, const can be applied to any Glish value. There are two general types of ``constness'' in Glish. If const occurs on the left hand side of the assignment operator, it indicates that the value cannot be modified or reassigned. Modification would be something like changing elements of a vector or fields of a record. Reassignment changes the whole value. When used on the right hand side of an assignment, it indicates that the value is constant, but that the Glish variable can be reassigned. This type of ``constness'' is most useful with records (§ 3.9.2) and references (§ 3.9.3).

const values are different from type constants which were discussed earlier. Type constants allow the user to explicitly specify a member of a particular type, but const values allow the user to name a particular constant, e.g. pi (see § 9.11).

Using const can help prevent important functions or values from being accidentally overwritten. Attempts to modify const values result in an error. const values, however, are not quit as constant as perhaps they should be, const values can be deleted with symbol_delete (§ 9.6). The following sections (§ 3.9.1-§ 3.9.3) explain const values in more detail.

Constant Arrays and Vectors

     

Typically, any Glish value is made const by putting the const keyword at the beginning of an assignment statement:

    const PI := 3.141592653589793238462643
After this, PI cannot be modified; attempts to modify it will result in error messages. Type constants are not the only place where this can be use; if the constant must be calculated:
    ident := array(0,4,4)
    ident[array(1:4,4,2)] := 1
    const ident := ident
the value can be made const after the calculation is done. Here it takes more than one line to set up ident, and after this is done it is made const.

   

Constant Records

 

  In the case of records, either the whole record can be const, individual fields can be const, or the field names can be const.

Constant Whole Record

Since a record as whole is just like arrays, vectors, or any other value in Glish, the whole record can be made const like other values:

    const rec1 := [ x=2.76, y=9.7102 ]
    rec2 := [=]
    rec2[1] := 8.261
    rec2[2] := 3.902
    const rec2 := rec2
With rec1 a record constant (see § 3.4.1, page gif), is assigned to the value, and it is made constant. After this, any attempt to reassign rec1, e.g. rec1 := 0, or to modify the fields, e.g. rec1.x := 0 or rec1.z := 0, will result in an error. rec2 will behave in the same way.

Constant Field Names

  Sometimes it is important to have a record whose fields can be used and modified but new fields cannot be added. One case where this might be useful is in handling two dimensional coordinates. In this case, the record will only contain two fields:

    coord := const [ x=0, y=0 ]
    coord.x := 3.451
    coord.y := 0.829
    coord2 := coord
Here two coordinates are created, coord and coord2. The fields that these contain can be modified, but an error results if an attempt is made to add new fields. Here it is not the values, coord and coord2, that are constant but rather the fields in the record. Either coord or coord2 could be reassigned, coord := 0.  

Constant Fields

  The fields of a record can also be made const. In this case, new fields can be added to the record, but some fields cannot be modified. For example:

    rec := [ one=1:4, const two=3.871 ]
    rec.three := [ 2.56, 1.639 ]
    const rec.four := 1984
In this example, fields one and three can be modified, but the rest cannot. rec can also be reassigned.

With const fields and const field names, records can be created so that the fields cannot be modified and new fields cannot be added:

    rec := const [ const x=2.3, const y=7.4 ]
    const rec2 := [ x=2.3, y=7.4 ]
Here rec is the same as rec2 except that rec can be reassigned.    

Constant References

   

  const whole value (see § 3.8.1, page gif) and partial value (see § 3.8.2, page gif) references can also be created. These allow the referenced value to be accessed, but not modified:

    var := 1:10
    vref := const ref var
    const cref := ref var
    var[2] := 90
In this example, as var changes so will vref and cref, but neither vref nor cref can be use to modify var. The difference between the two is that vref can be reassigned but cref cannot. Partial value references can be created and used in the same way.

Once a reference is constant, subsequent references to the constant reference are themselves constant. In this example:

    var := 1:10
    ref1 := ref const ref var
    ref2 := const ref var
    ref3 := ref ref2
ref1, ref2, and ref3 are all constant references, and as a result, they cannot be used to modify var.    


next up previous contents index
Next: Copy On Write Up: ValuesTypes, and Constants Previous: References

Thu Nov 13 16:44:05 EST 1997