Mutable lexical variables
Factor handbook » The language » Lexical variables

Prev:Lexical variables in literals
Next:Lexical variables and fry

When a lexical variable is bound using :>, ::, or [|, the variable may be made mutable by suffixing its name with an exclamation point ( ! ). A mutable variable's value is read by giving its name without the exclamation point as usual. To write to the variable, use its name with the ! suffix.

Mutable bindings are implemented in a manner similar to that taken by the ML language. Each mutable binding is actually an immutable binding of a mutable cell. Reading the binding automatically unboxes the value from the cell, and writing to the binding stores into it.

Writing to mutable variables from outer lexical scopes is fully supported and has full closure semantics. See Examples of lexical variables for examples of mutable lexical variables in action.