Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
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 reassigned or modified (changing elements of a vector or fields of a record). Reassignment changes the whole value. When const occurs 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).
The const values are different from type constants that were discussed earlier. Type constants allow you to explicitly specify a member of a particular type, e.g. 2.361, but const values allow you to make a particular value unmodifiable.
Using const can help prevent important functions or values from being accidentally overwritten. Attempts to modify const values result in an error. However, const values are not quit as constant as perhaps they should be, const values can be deleted with symbol_delete (§ 10.6). The following sections (§ 3.9.1-§ 3.9.3) explain const values in more detail.
Typically, any Glish value is made const by putting the const keyword at the beginning of an assignment statement:
const PI := 3.141592653589793238462643Now PI cannot be modified; attempts to modify it will result in error messages. Type constants are not the only place where this can be of use; if the constant must be calculated, for example:
ident := array(0,4,4) ident[array(1:4,4,2)] := 1 const ident := identthe value can be made const after the calculation is complete. In this case, it takes more than one line to set up ident, and after this is done it is made const.
In the case of records, the whole record can be const, individual fields can be const, or the field names can be const.
const rec1 := [ x=2.76, y=9.7102 ] rec2 := [=] rec2[1] := 8.261 rec2[2] := 3.902 const rec2 := rec2Here rec1 is initialized to a record value and made const in one statement. 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
, results in an error.
rec2 behaves in the same way.
coord := const [ x=0, y=0 ] coord.x := 3.451 coord.y := 0.829 coord2 := coordHere 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 can be reassigned,
coord := 0
.
rec := [ one=1:4, const two=3.871 ] rec.three := [ 2.56, 1.639 ] const rec.four := 1984In this example, fields one and three can be modified, but the rest cannot. Also, rec can 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.
It is good to note that const whole value (see § 3.8.1, page ) and partial value (see § 3.8.2, page ) 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] := 90In 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 ref2ref1, ref2, and ref3 are all constant references, and as a result, they cannot be used to modify var.