let
let declares a fixed binding: a name that keeps the value it is declared with.
Details
- A
letneeds an initializer. - Assigning a
letname is a compile error wherever the assignment is written: in a nested block, a lambda, agoordeferblock, or another function. letfixes the binding, not the value. The fields, elements, and entries of aletvalue can still change.- Function parameters, a lambda's
x, and every name aforbinds areletbindings too. Only a var can be reassigned.
Basic examples
let limit = 100
limit * 2 // 200Write the type after the name when the initializer does not give the one you want, such as an empty array:
let scores: Array[int] = []
scores.length() // 0A program that uses only fixed names:
let name = "Ant"
let greeting = "Hello, ${name}"
println(greeting)Hello, AntScope
The type may be wider than the initializer's, such as a union or a nullable type:
let id: int | string = 7
id is int // true
let nickname: string | null = null
nickname ?? "none" // "none"A record destructuring binds fields by name, and { field: name } renames one:
type Point = { x: int, y: int }
let { x, y: top } = Point{ x: 3, y: 4 }
x + top // 7An array destructuring binds elements by position, and ... binds the rest as a new array:
let [first, ...rest] = [1, 2, 3]
first // 1
rest // [2, 3]A let in a block is visible to the end of that block. An inner block, such as a function body, may declare the same name, which hides the outer one there:
let label = "outer"
func describe(): string {
let label = "inner"
label
}
describe() // "inner"
label // "outer"A function value bound with let can call itself, since its body runs only after the binding exists, as long as its type is written, here by the return type : int:
let factorial = func(n: int): int {
if n <= 1 { 1 } else { n * factorial(n - 1) }
}
println(factorial(5))120A select head can bind its result with let:
let ready: Channel[string] = Channel.new(1)
Channel.send(ready, "go")
let message = select {
let m = Channel.receive(ready) { m ?? "closed" }
Time.sleep(1s) { "timeout" }
}
println(message)goA library exports a let with export let. The importer reads it and cannot assign it:
export let MaxRetries = 3import "./config.ant" { MaxRetries }
for attempt in Array.range(1, MaxRetries + 1) {
println("attempt ${attempt}")
}attempt 1
attempt 2
attempt 3Applications
Name an intermediate result that the code uses twice:
let words = "the quick brown fox jumps" | String.split(" ")
let longest = words.map({ x.length() }).max()
words.filter({ x.length() == longest }) // ["quick", "brown", "jumps"]Each iteration of a for binds a fresh let, so a closure made in the body keeps that iteration's value:
let handlers: Array[func(): int] = []
for i in [1, 2, 3] {
handlers.push({ i * 10 })
}
handlers.map({ x() }) // [10, 20, 30]A top-level nullable let stays narrowed inside a function that tests it, since nothing can reassign it:
let greeting: string | null = "hi"
func shout(): string {
if greeting == null {
return ""
}
greeting.toUpper()
}
println(shout())HIWith var greeting, the call greeting.toUpper() is a compile error: only a let narrows inside a function that tests it.
Properties and relations
A let fixes the binding, not the value. A let array still grows:
let xs = [1]
xs.push(2)
assert(xs == [1, 2])A let record's fields can still be assigned:
type Counter = { count: int }
let c = Counter{ count: 0 }
c.count += 1
assert(c.count == 1)Possible issues
Assigning a let is a compile error. Declare the name with var when it changes:
let total = 0
for n in [1, 2, 3] {
total += n // error: cannot assign to total, a let; declare it with var to reassign it
}var total = 0
for n in [1, 2, 3] {
total += n
}
println(total)6An assignment inside a lambda counts too, even if the lambda never runs:
let count = 0
let bump: func(): void = { count = count + 1 }
// error: cannot assign to count, a let; declare it with var to reassign itA parameter, a lambda's x, and a for variable are let bindings, so assigning one is a compile error. Bind a var to hold a value that changes:
func double(n: int): int {
n = n * 2 // error: cannot assign to n, a parameter; declare a var to hold a value that changes
n
}
for s in ["a"] {
s = s.toUpper() // error: cannot assign to s, a for variable; declare a var to hold a value that changes
}A let in an array destructuring assignment is an assignment too, so a swap needs two vars:
var a = 1
let b = 2
[a, b] = [b, a] // error: cannot assign to b, a let; declare it with var to reassign itA function value that calls itself needs its type written. Without a return type, its body would be inferred from a call to itself:
let factorial = func(n: int) {
if n <= 1 { 1 } else { n * factorial(n - 1) } // error: factorial is used by a function in its own initializer; state its type
}Declaring a name twice in one scope is an error, even with let both times:
let limit = 10
let limit = 20 // error: limit is already declared in this scopeA let may not take a prelude name, in any scope:
let println = "hi" // error: println is a prelude name and cannot be redeclaredAn array destructuring with more positions than the array has elements panics:
let [a, b] = [1] // panic: a destructuring needs 2 elements, got 1See also
var · for · select · export · func · Assignment · Variables tour · Idiomatic Ant
