Mutable Module Variables
Introduction
Apart from I/O Io is really a 'pure' language, there is no mutable state and as a consequence no side effects. This is very attractive from a theoretical point of view as it makes reasoning about the language both easy and elegant. To remove this feature compromises the beauty of Io and makes 'inappropriate' programming styles possible. For practical reasons I have still chosen to do so, Amalthea adds mutable variables to Io. I've tried to make the use both explicit and limited to encourage making do without them:
- They are only available at the module level
- Reading and writing them is syntactically explicit as they live in a separate namespace
- They can't be imported or exported between modules
- They are not available by default, a switch to Amalthea enables them
I strongly suggest that you don't use mutable variables, they are only included in Amalthea to support implementations of microthreads, coroutines, exceptions, and similar control structures with less syntactical overhead.
Using Variables
To be able to use variables in Amalthea you need to start it with a switch, -vars.
Mutable variables are declared at the module level along with other declarations. The syntax to do so is:
variable variable identifier: initial continuation.
An example is:
variable debug_flag: 0.
As variables live in a different namespace than other identifiers it's entierly possible to have both declared continuations and declared variables with the same name. This is however adviced against. Two operations aravailable on variables, reading and writing them. Reading them looks like this:
variable identifier => identifier; continuation
While writing looks like:
variable identifier <= continuation; continuation
Reading will simply bind the continuation that's currently in the variable to the identifier in the continuation supplied and the calls this. Writing rebinds the variable to the first continuation supplied, then calls the second continuation with no parameters. An example module to supply a sequence of unique numbers could look like this:
variable number: 0.
declare next_number: -> return;
number => n;
+ n 1 -> nn;
number <= nn;
return n.