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
typeOfreturns'Ref'for reference values- Attempting to
loada value that is not of typeRefthrows an error - Attempting to
storeinto a value that is not of typeRefthrows 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
refcreates a reference object pointing to an existing variable and returns a value of typeRefloaddereferences and reads the current value of the variable a reference points tostoremodifies 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
refandHastake aVariableas their argument:refreferences a simple variable in the current scope, whileHas <Variable name>checks whether the specified variable exists in a mutable stack
7. Purpose
- Indirect access: manipulate variables indirectly through references, suitable for scenarios that require passing variable "pointers"
- Data exchange: conveniently exchange variable values across different scopes or functions
- Mutable state management: modify external state from within an enclosing context through references