2.3 Fields
var
prefix must be used with the field declaration. The presence of the var
annotation triggers generation of the set accessor method. Immutable fields receive their values either as part of object creation or by an initializing expression associated with the field declaration; see section 2.3.4. Note that the contents of an immutable field can itself be mutable, but the binding of the field to its contents cannot change. (Global and local variables in Cecil similarly default to initialize-only semantics, with an explicit var
annotation required to allow updating of the variable's value, as described in section 2.5.2.)In general, we believe that it is beneficial to explicitly indicate when a field is mutable; to encourage this indication, immutable fields are the default. Programmers looking at code can more easily reason about the behavior of programs if they know that certain parts of the state of an object cannot be side-effected. Similarly, immutable fields support the construction of immutable "value" objects, such as complex numbers and points, that are easier to reason about.
Many languages, including Self and Eiffel, support distinguishing between assignable and constant variables, but few imperative languages support initialize-only instance variables. CLOS can define initialize-only variables in the sense that a slot can be initialized at object-creation time without a set accessor method being defined, but in CLOS the slot-value
primitive function can always modify a slot even if the set accessor is not generated.
Generated with Harlequin WebMaker