Variables
A let is a name that is never reassigned; a var is a name that may be. The keyword says nothing about the
data behind the name: a field store, a push and a callee that writes its parameter reach through a let
to the record it names. A number, bool, enum or String has no record behind its name, so changing one is
reassigning it, and it needs a var.
Mutable Variables (var)
Section titled “Mutable Variables (var)”typealias Tally = int(0 to u64.max)
var x = 42 as Tally // a Tally: the cast names the typex = x + 5 // reassignment allowed, range-checked against Tally// var y = 42 // E2004: A var cannot start from a bare number: write 'var name = expr as Type'.A var holding a number names its alias where it is born, locally, at file scope and as a static. A bare
number, arithmetic over bare numbers (16 * 1024), a plain let constant, and an array, map or tuple literal
of bare numbers are refused as its initializer (E2004); cast it (var size = (16 * 1024) as ByteCount).
A var initialized from a value of some alias is that alias.
Fixed Variables (let)
Section titled “Fixed Variables (let)”let pi = 3.14159let name = "Maxon"// pi = 3.14 // E2013: 'pi' is a let and cannot be reassignedA let bound to a bare number, to arithmetic over bare numbers or to a literal container of them is a
constant: it acts like the literal it holds, and fits any alias its value fits, checked at compile time
at each use. A let computed from such constants or from a loop counter between two literal bounds carries
the range its value can have, and each use is checked against it the same way, for integers and floats
alike. A let of a number whose range the compiler cannot know (let w = 1 shl k) is E2004; write
let w = (1 shl k) as Width. A literal cast (let port = 8080 as Port) makes the let a value of the
alias; when every use of it would take the plain constant the same way, the cast is E3010 (see
Construction).
typealias Port = int(0 to 65535)
function open(p Port) returns Port return pend 'open'
function main() returns ExitCode let base = 8000 let port = base + 80 // a constant: 8080 print("{open(port)}\n") // 8080 // open(base * 10) // E3005: Value 80000 is outside the range of 'Port' (int(0 to 65535)) return 0end 'main'A let fixes which record the name holds, not what is in the record. A field store, a method that writes its
receiver (push, append, set, remove, clear, …) and a callee that writes its parameter all reach
through a let:
typealias Count = int(0 to 1000)typealias Counts = Array with Count
type Box export var n as Count
static function create(n Count) returns Self return Self{n: n} end 'create'end 'Box'
function main() returns ExitCode let p = Box.create(1) p.n = 2 // a field store through a let let xs = Counts.create(0) xs.push(p.n) // so is a method that writes its receiver // p = Box.create(3) // E2013: 'p' is a let and cannot be reassigned print("{p.n} {xs.count()}\n") // 2 1 return 0end 'main'What a let refuses is reassignment:
- Assigning to it is E2013 (
'p' is a let and cannot be reassigned). The same wording covers aforvariable, amatchorif letbinding and a tuple element bound bylet. Aletfield is'version' is a let field and cannot be reassigned. - Passing it to a
mutableparameter is E2013 too, because that parameter reassigns the caller’s variable:'n' is a let and cannot be passed to the `mutable` parameter 'x'; bind it to a `var`. - A number,
bool, enum orStringheld by aletcannot change at all, since changing one is reassigning it. Declare itvarwhen it must.
A name bound from a let names the same record, so a write through either name is seen through the other. A
let bound from state that something else writes sees the writes:
typealias Count = int(0 to 1000)
type Box export var n as Count
static function create(n Count) returns Self return Self{n: n} end 'create'end 'Box'
type Holder export var box as Box
static function create() returns Self return Self{box: Box.create(1)} end 'create'end 'Holder'
function main() returns ExitCode let r = Box.create(5) let c = r c.n = 7 // r names the same record let h = Holder.create() let b = h.box h.box.n = 8 // b sees it print("{r.n} {b.n}\n") // 7 8 return 0end 'main'Data that nothing may write is a separate notion from the keyword: constant data, frozen data, a stored Map
key and a SharedValue read stay read-only wherever they are bound (see Read-Only Values
and Constant Data).
-
Every variable is initialized where it is declared, and its type is inferred from the initializer. A declaration takes no type annotation:
var x as Tally = 0is E2010 (Expected '=' but got 'as'). A cast names the type instead —var x = 0 as Tallydeclares aTally; a file-scopelettakes the same cast (let limit = 5 as Tally). -
A
varis born a value of its alias, and every assignment to it is checked against it (see Range Checks). -
A
vartakes the brand of the first branded value stored into it when its initializer has none:var a = [1 as Item, 2 as Item]followed bya = xsfor anXsmakesaanXsfor its whole life, and a later store of aYsis E3005 (cannot assign a value of type 'Ys' to variable 'a', which holds 'Xs'). The first store in source order decides. See Generic-Instance and Function-Type Aliases Are Brands. -
Variables are block-scoped.
-
A
varthat is never reassigned is E3077 (variable 'x' is never reassigned; use 'let' instead of 'var'), whatever else is done with it. Reassigning the name is what keeps avar: an assignment, passing it to amutableparameter, or calling a method declaredmutable selfon a value of a value type (text.append("!")over aString). A field store, apush, a call whose callee writes the parameter and moving it into another binding leave the name unreassigned, so each is written withlet. Avarbound to an array, map or tuple literal that is written through is writtenlet items = [..].clone(): a plainletof a literal is constant data, and writing through it is E3207. -
Every variable must be used (E3012). This covers
let/var, parameters, loop variables, pattern bindings and closure parameters. Every unused variable of a function is reported, in source order. The name_discards: it creates no binding and may appear several times in one pattern (for (_, _) in pairs). A parameter named_takes its argument unnamed, so reading_in the body is E2004, and one signature may declare several. In a destructuring,_discards its element:let (a, _) = tdeclaresaalone, and(let _, let b) = tdeclaresbalone. Names such as_xare ordinary variables. A method that implements an interface requirement is exempt for its parameters, since the interface dictates them. -
Records are shared, not copied.
let b = afor a record-typedahandsbthe same record; usea.clone()for an independent copy (see Reference-by-Default Assignment). Numbers,booland other scalar values are always independent copies. -
let b = amovesawhenais avar. Whenais alet,bnames the same record andastays usable.a.clone()is the independent copy (see Moves and Sharing):typealias Coord = int(i64.min to i64.max)type Pointexport var x as Coordstatic function create(x Coord) returns Pointreturn Point{x: x}end 'create'end 'Point'function main() returns ExitCodevar a = Point.create(1)let b = a // a MOVES into b; reading a afterwards is E3102b.x = 10a = Point.create(2) // assigning a makes it usable againlet c = Point.create(3)let d = c // d names c's recordlet e = c // so does e, and c stays usablee.x = 30let f = c.clone() // an independent recordf.x = 40print("{a.x} {b.x} {c.x} {d.x} {f.x}\n") // 2 10 30 30 40return 0end 'main'
Top-Level Bindings
Section titled “Top-Level Bindings”A let can be declared at file scope, outside any function:
typealias Tally = int(0 to u64.max)
let START = 10 as Tallylet MAX_SIZE = 1024let names = ["ada", "alan"]let dataFile = FilePath from "data.txt"
function main() returns ExitCode print("{START} {MAX_SIZE} {names.count()} {dataFile.filename()}\n") // 10 1024 2 data.txt return 0end 'main'letdeclares a constant any function in its directory’s subtree can read. A file-scopevar, with or withoutexportbefore it, is a syntax error: state a function updates is a localvarpassed to amutableparameter, a field of a record, aSharedValue, or the fields of a service.- A binding shares its directory’s one namespace with every other top-level name (see
Bare Names and Ambiguity), and another directory’s binding is read through
its directory:
alpha.LIMIT. - An initializer is a value the compiler knows before the program runs, and it calls nothing: literals,
arithmetic, casts, enum and union cases, array, map and tuple literals,
Type from "literal",ArrayAlias from [...], a function name (let step = countdown), and another top-levellet, declared before or after it, in this file or one it can see (let B = A). A call is E3220 (the initializer of 'shared' calls 'Cache.create': a module-level or staticletinitializer calls nothing, so write a literal, or build the value where it is used), and a call in the initializer of a number,boolor enum binding is E2045 (Function calls are not allowed in global variable initializers). A closure literal’s body is not part of the initializer:let total = function(n int) gives helper(n)builds a function value, andhelperruns whentotalis called. A call that receives the closure,wrap(function(n int) gives helper(n)), is still a call the initializer makes. - An initializer is outside every function, so a
returnin it is E2080, naming the binding. - A binding that holds a scalar — a number, a
boolor an enum case — is a compile-time constant, inlined at every use. Its initializer must have a value known at compile time, as a body knows2 * WIDTHorsqrt(4.0). A derived constant keeps its operands’ typealias, as in a body: withlet a = 5 as Small,let s = a + ais aSmall. - A name in an initializer resolves as it does in a function body: a name nothing declares is E3004, one the file cannot see is E3008, and one several directories declare is E3095. That one diagnostic covers every read and assignment of the binding. A tuple assignment naming a module-level binding is E2015; assign the element to a local and store that.
- Nothing runs before
mainon behalf of a top-levellet. Initializers that depend on each other in a cycle are E2012, and their order in the source does not matter. Alet’s value is image data, laid down in read-only memory as one copy that every function and thread reads, and it isimmutable(see Constant Data);.clone()gives a mutable copy where one is needed. A static field follows the same rules (see Static Fields). - A struct literal at file scope is E3076; declare a
static letholding the literal inside the type (static let origin = Self{x: 0, y: 0}), or build the record where it is used. - An initializer whose value has an interface type is E2015: an interface value is two words, a value and its witness, and a global holds one. Return a concrete type from the initializer, or hold the interface in a function’s local.
- A tuple literal is accepted in an initializer wherever a function body accepts one, with the same type.
let pair = (1, "one")is a tuple of the types its elements have; a slot that states a tuple type — an element ofArrayAlias from [...](let steps = Steps from [(0, 1), (2, 3)]overtypealias Steps = Array with Stepandtypealias Step = (Word, Word)), a struct literal’s field (static let origin = Holder{step: (7, 9)}), a factory parameter, a union payload, or an element of another tuple — gives each element that slot’s type. A tuple literal inside extra parentheses is the same tuple, and avarborn from a tuple of bare numbers is refused as it is in a body. - A module-level
letisimmutableimage data (see Constant Data): a write through it is E3207, and storing it where something may write it is E3196. - A
spawnin a global initializer is E3220, like any call; start services inmain, or declare a program-wide one withdefault. AKey.current()orKey.register()in a global initializer is E3173: the defaults are built after every global initializer has run. - An unmarked top-level declaration is visible to its directory’s subtree;
exportandpublicwiden it to every file (see Visibility).