Skip to main content

Reference System

Alia's reference system provides three keywords — ref, load, and store — for creating and manipulating variable references.

1. Reference Keywords​

ref — Creating a reference​

ref <Variable name>

ref creates a reference object pointing to an existing variable and returns a value of type Ref.

load — Reading a reference​

load <Value ref>

load dereferences and reads the current value of the variable a reference points to.

store — Writing through a reference​

store <Value ref> <Value val>

store modifies the value of the variable a reference points to.

2. Basic Usage​

Creating a reference and reading the value​

let x = 42
let r = ref x
print load r ~ prints 42

Writing a value through a reference​

let y = 10
let r2 = ref y
store r2 99
print y ~ prints 99

Multiple references to the same variable​

let z = 5
let r1 = ref z
let r2 = ref z
store r1 10
store r2 20
print z ~ prints 20

3. Advanced Usage​

Modifying a variable through a reference inside a loop​

let counter = 0
let r = ref counter
for { let i = 0 } i < 10 { i ++ } {
store r counter + 1
}
print counter ~ prints 10

Modifying a variable through a reference inside a conditional branch​

let value = 100
let r = ref value
if true {
store r 200
}
print value ~ prints 200

Chained operations through references​

let a = 1
let b = 2
let ra = ref a
let rb = ref b
let va = load ra
let vb = load rb
store ra ((va as Number) + (vb as Number))
print a ~ prints 3

Swapping variable values through references​

let x = 'hello'
let y = 'world'
let rx = ref x
let ry = ref y
let temp = load rx
store rx load ry
store ry temp
print x ~ prints 'world'
print y ~ prints 'hello'

4. Type Information​

  • typeOf returns 'Ref' for reference values
  • Attempting to load a value that is not of type Ref throws an error
  • Attempting to store into a value that is not of type Ref throws an error
let x = 42
let r = ref x
print typeOf r ~ prints 'Ref'

5. Error Handling​

~ Referencing an undefined variable throws an error
let r = ref nonexistent ~ Error

~ load on a non-reference value throws an error
let x = 42
load x ~ Error

~ store on a non-reference value throws an error
let y = 10
store y 100 ~ Error

6. Mechanism Notes​

  • ref creates a reference object pointing to an existing variable and returns a value of type Ref
  • load dereferences and reads the current value of the variable a reference points to
  • store modifies the value of the variable a reference points to
  • References follow variable scoping rules: you cannot reference a variable that is not accessible in the current scope
  • Both ref and Has take a Variable as their argument: ref references a simple variable in the current scope, while Has <Variable name> checks whether the specified variable exists in a mutable stack

7. Purpose​

  1. Indirect access: manipulate variables indirectly through references, suitable for scenarios that require passing variable "pointers"
  2. Data exchange: conveniently exchange variable values across different scopes or functions
  3. Mutable state management: modify external state from within an enclosing context through references

Next Steps​