Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

References & Dereferencing

A reference is the address (slot number) of a variable.

SyntaxMeaning
&xthe reference to variable x
*rthe current value of the variable that r refers to
x = 10
r = &x
println(*r)      # 10
x = 20
println(*r)      # 20
println(*r + 1)  # 21

A reference is stored as a plain integer, so println(r) prints a slot number. *r can be used anywhere an expression is allowed (assignments, arguments, conditions):

if *r == 20
  println('twenty')
endif

Rules

  • Assigning through a reference (*r = 5) is not supported and is a compile error.
  • Dereferencing a reference to a variable that was never assigned stops the program.
  • References are how built-ins return extra values. httpRequest writes the response body into the variable you pass with &, see Standard Library.