Pelago
Ownership

Ownership and memory

Value semantics, moves, copies, access modes, cleanup and boxes: how Pelago manages memory with no garbage collector and no borrow checker.

Pelago has no garbage collector and no borrow checker. Every value has exactly one owner, values never share mutable state, and memory is released at a point you can read in the source. This chapter has one page per idea.

IdeaWhat it answersPage
Value semantics, copyWhat does b = a do? When do I get two values?Values and copies
MovesWhich assignments give a value away, and what the compiler says if you use it again.Moves
Read accessThe default parameter mode: look, do not touch.Read access
inout and x!Change the caller's variable in place.inout
sink and x!!Take the value away from the caller.sink
deinit, scope exit, --leak-checkWhen values are released and how to run code at that moment.Cleanup
box<T>A value on the heap: recursive types and indirection.Boxes
deferRun a statement when a block ends.defer

The rules in one place

  1. A name owns its value. Two names never own the same value.
  2. Plain data (numbers, bool, char, and structs and tuples made only of them) is copied by assignment. Data that owns memory (strings, lists, maps, boxes, and anything containing them) is moved by assignment.
  3. A parameter is one of three modes: read (the default), inout or sink. The call repeats inout and sink (or the marks ! and !!), so the effect of a call is visible without opening the function.
  4. References exist only for the length of a call or statement. They cannot be stored, returned or captured, which is why there are no lifetime annotations.
  5. A value is released when its owner goes out of scope, running its deinit if it has one. Nothing else frees memory.

Because two names never share one mutable value, a function cannot change your data behind your back, and tasks cannot race on it. See Concurrency.

Where this shows up elsewhere

  • Method marks follow the same idea: xs.push!(1) changes the receiver, task.join!!() uses it up. See Functions.
  • Closures list their captures with the same marks. See Functions.
  • Wrong use is caught at compile time with a message naming the line and the fix. Each page below shows the exact text.

On this page