Values, mutation and resources
Weaver has no garbage collector, and no lifetimes to write. Its model rests on one idea: every value behaves as if it were copied. The compiler makes that cheap, by sharing what nobody modifies and moving what is no longer used.
Values, not references
Section titled “Values, not references”Assigning or passing a value never creates a second name for the same
thing that could change behind your back. When a binding modifies a value
that another one still uses, the copy is written, .copy():
import weaver.io.print
type Team = { name: String, members: String[] }
fn main() { val core = Team{name: "core", members: ["ada", "alan"]} var tools = core.copy() tools.name = "tools" tools.members.push("grace") print("${core.name}: ${core.members}") print("${tools.name}: ${tools.members}")}core: ["ada", "alan"]tools: ["ada", "alan", "grace"]Without .copy(), the compiler stops: the binding is modified, and its
value is another’s: write .copy(). Copies are never hidden in an
assignment, and nothing changes at a distance: there is no aliasing bug to
look for. A value that is no longer used afterwards is moved instead, at
no cost, and one that nobody modifies is shared.
Modifying a parameter: mut and &
Section titled “Modifying a parameter: mut and &”A parameter is read-only, and passing it copies nothing. A function that
modifies its argument declares it mut, and the caller writes &, so
that a modification is visible where it happens:
import weaver.io.print
fn addAll(xs: mut Int[], n: Int) { for i in 0..xs.len() { xs[i] += n }}
fn main() { var scores = [1, 2, 3] addAll(&scores, 10) print("${scores}")}[11, 12, 13]A method that modifies its receiver is declared with mut self, and the
call needs no &: stack.push(x) on a var.
Returning what you borrowed
Section titled “Returning what you borrowed”A function that returns part of a parameter it only borrowed copies it, in writing too:
import weaver.io.print
type User = { name: String, email: String }
fn nameOf(u: User): String = u.name.copy()
fn main() { val u = User{name: "Ada", email: "ada@example.org"} print(nameOf(u))}AdaA value built in the function, or received with sink, is moved rather
than copied. In an editor, wv lsp shows at the end of each line what it
costs: heap when it allocates, copy when it copies.
Resources: using
Section titled “Resources: using”A file, a socket or a task is a resource: something that must be
released. Bound with using, it is released at the end of its block,
whatever the path that leaves it, and an error in releasing it is
reported like any other:
import weaver.io.printimport weaver.io.Fileimport weaver.{Fs}
fn main() ! { { using f = File.create("notes.txt")! f.write("written, then closed\n")! } print(Fs.readText("notes.txt")!.trim()) Fs.remove("notes.txt")!}written, then closedA resource that a path forgets, neither released nor passed on, is a
compile error, not a leak. defer runs a statement when the block ends,
and errdefer only when it ends on an error.
Going further
Section titled “Going further”Understanding memory shows, in diagrams,
what is behind these rules: who owns each block, and when it is freed. The
memory model and
resources chapters of the reference give the
exact rules: sink parameters, views of strings and arrays, arenas, and
what the compiler counts.