Declaring a value
Naming a value with var, freezing it with const, and the := arrow that sets them.
Two words are enough to hold something: var for what may change, const for what must not.
@entry(start)
object MyProgram {
start() {
var greeting := "Good day";
greeting := "Hello";
const NAME := "Simon Magus";
Console.print(text: greeting + ", " + NAME + "!");
}
}
Run it: Hello, Simon Magus!. The greeting was “Good day” before it was “Hello”; the name has
not moved, and will not.
var — a name on a value that may change
var greeting := "Good day"; declares greeting and gives it a text. The next line changes it:
greeting := "Hello";. It is the same arrow both times, and that is no accident — declaring and
reassigning are the same gesture, putting a value under a name.
The type is not spelled out, it is followed. You wrote nowhere that greeting is a text; the
compiler reads that off the quotes. It is settled for good all the same: greeting is a text for
its whole life, and assigning it anything else further down would be refused. Nothing stops you
from writing it anyway, when you want to be explicit:
var greeting: str := "Good day";
What you gain then is not safety — that was there already — it is telling the reader, without making them work it out, what this variable holds.
const — a value that will not move again
const NAME := "Simon Magus"; declares the same thing, except that the compiler will refuse any
attempt to change it. Try adding NAME := "somebody else"; and read the answer:
error[D001]: 'NAME' is declared 'const': its value cannot change
That is not a courtesy, it is a guarantee: whoever reads the rest of the program knows NAME
still holds what it held at its declaration, without having to read everything in between. The
type is optional there as it is on var — const G := 9.81; is enough.
Putting texts together with +
The last line deserves a word. Console.print accepts exactly one text, and we have two to
show — plus the punctuation between them. The + sign joins them end to end:
greeting + ", " + NAME + "!"
Four pieces glued into a single text: the variable, a comma, the constant, an exclamation mark.
That is what print receives.
And it is heavy going. Four pieces for a three-word sentence, with quotes to open and close
around every scrap of punctuation, and spaces you have to remember to put inside the quotes —
forget the one in ", " and you get Hello,Simon Magus. The sentence you want to read appears
nowhere in one piece: it is in parts, and it is up to the reader to reassemble it in their head.
There is a lighter spelling, one that shows the sentence as it will come out. That is the
subject of the next sub-section — until then, + does the job.
:=, and not =
You have had it in front of you since the top of the page: Archon writes := to assign, at
the declaration as at the reassignment. The = sign on its own does not assign — it is reserved
for another use, which we will come to in due course.
Remember the arrow for now: wherever you want to put a value under a name, it is :=. And if
the slip gets past you, the compiler will say so rather than guess on your behalf.
Your turn
- Change
var greetingintoconst greetingand run: the compiler names the offending line and the reason. - Declare
var city := "Alexandria";then, further down,city := 12;. The refusal will tell you that a variable’s type is settled at its declaration. - Declare the same name twice —
var greeting := "Good evening";a second time. A name is not reused within the same scope, and the compiler names the one already taken.