mutabilitas
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
MU-1 baseline narrowed by tcs1: in-place list mutation requires a mut binding.
Syntax: mut list<int> xs
Category#
binding
Related#
Examples#
radix/corpus/reference/concept/mutabilitas/shallow-const.fab (canonical · concept)#
MU-1 baseline narrowed by tcs1: in-place list mutation requires a mut binding.
# =============================================================================
# mutabilitas — in-place list mutation needs mut (D-1 shallow law, tcs1)
# =============================================================================
main {
mut list<int> xs ← []
xs.append(4)
print xs.length()
}Expected output:
1
radix/corpus/reference/reject/mutabilitas/binder-const-retired-reject.fab (supporting · reject)#
A loop binder is let or mut; the old const binder is retired.
# =============================================================================
# mutabilitas — const binders are retired
# =============================================================================
# Binders take `let` (a fixed name) or `mut` (a writable name):
# `for from xs let x { ... }`. `const` states a compile-time fact and goes
# with `=`.
main {
let list<int> xs ← [1, 2, 3]
for from xs const x {
print x
}
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/binder-ref-refused-reject.fab (supporting · reject)#
A binder names a value per iteration; ref cannot introduce one.
# =============================================================================
# mutabilitas — ref is refused as a binder
# =============================================================================
# A binder is `let` or `mut`. `ref` re-points a name at a place; use it for a
# local or a parameter, not for a loop binder.
main {
let list<int> xs ← [1, 2, 3]
for from xs ref x {
print x
}
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/binding-type-slot-reject.fab (supporting · reject)#
every declaration writes its type slot: let x ← 5 has none (write let int x, or let _ x to infer).
# =============================================================================
# mutabilitas — no type-omitted declaration
# =============================================================================
# There is no short declaration form. Every binding word writes its type slot,
# and `_` is the explicit request to infer: `let _ x ← 5`. The same holds for
# `mut x`, `ref x` and `const x`. Binders (`for … let j`) are bound to a thing
# that carries the type and write no slot.
main {
let x ← 5
print x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/call-marker-mismatch.fab (supporting · reject)#
A call marker that differs from the parameter marker is call_marker_mismatch.
# =============================================================================
# mutabilitas — call marker mismatch (SEM057)
# =============================================================================
# This exemplum intentionally fails to compile.
fn bump(mut list<int> xs) → void {
xs.append(1)
}
main {
mut list<int> a ← [1, 2, 3]
bump(own a)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/call-marker-missing.fab (supporting · reject)#
A mut parameter called without the mut marker is call_marker_missing.
# =============================================================================
# mutabilitas — call marker missing (SEM057)
# =============================================================================
# This exemplum intentionally fails to compile.
fn bump(mut list<int> xs) → void {
xs.append(1)
}
main {
mut list<int> a ← [1, 2, 3]
bump(a)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/call-marker-on-read-only.fab (supporting · reject)#
A write or ownership marker on a read-only parameter is call_marker_on_read_only.
# =============================================================================
# mutabilitas — call marker on read only (SEM057)
# =============================================================================
# This exemplum intentionally fails to compile.
fn peek(list<int> xs) → void {
print xs.length()
}
main {
let list<int> a ← [1, 2, 3]
peek(mut a)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/const-loop-binder-field-store-reject.fab (supporting · reject)#
A let loop binder is written once by the loop; a store through it is a second assignment.
# =============================================================================
# mutabilitas — no store through a let loop binder
# =============================================================================
# A loop binder is `let` (a fixed name, written once by the loop head) or
# `mut` (a writable name). Writing through a `let` binder is a second
# assignment; declare the binder `mut` to write through it.
class P {
mut int x = 0
}
main {
mut list<P> ps ← [P { x = 1 }]
for from ps let q {
q.x ← 7
}
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/const-mutating-receiver-reject.fab (supporting · reject)#
In-place collection methods rewrite the receiver, so a let binding refuses them.
# =============================================================================
# mutabilitas — mutating receiver methods need a writable binding
# =============================================================================
# `.put`, `.add` and `.append` rewrite the receiver in place. A `let` binding
# is read-only, so each call below is a SEM020 reject; bind `mut` to mutate.
main {
let map<string, int> m ← { "a": 1 }
m.put("b", 2)
let set<int> s ← empty
s.add(2)
let queue<int> q ← empty
q.append(1)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/const-runtime-reject.fab (supporting · reject)#
const is compile-time only: a run-time const declaration is retired (write let).
# =============================================================================
# mutabilitas — run-time const is retired
# =============================================================================
# `const` states a compile-time fact and goes with `=`. A run-time name that is
# written once is `let`: `let int x ← 5`.
main {
const int x ← 5
print x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/const-share-into-var-reject.fab (supporting · reject)#
A let list is read-only all the way down and cannot be shared into a mut name (SEM065).
# =============================================================================
# mutabilitas — let shared into mut (need 38a83ac8, rule 2)
# =============================================================================
# `mut list<int> ys ← xs` with `xs` a `let` would let `ys.append(2)` change `xs`.
# Write `← copy xs` for an independent value or `← own xs` to take it over.
main {
let list<int> xs ← [1]
mut list<int> ys ← xs
ys.append(2)
print xs
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/copy-channel-reject-la.fab (supporting · reject)#
A deep copy of a class holding a channel is refused with the member path.
# Duplicating this class would silently share its live `channel`; the compile-time
# refusal names the member that holds the identity-bearing resource.
class Capsa {
mut channel payload
}
main {
let channel source ← call 'runtime:echo' ("salve")
let Capsa fasciculum ← Capsa { payload = source }
mut Capsa duplex ← copy fasciculum
print duplex.payload
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/copy-channel-reject.fab (supporting · reject)#
A deep copy of a class holding a channel is refused with the member path.
# Copying a class would silently share its live channel. It is refused before
# runtime, and the diagnostic names the field containing that identity-bearing
# resource.
class Envelope {
mut channel payload
}
main {
let channel source ← call 'runtime:echo' ("hello")
let Envelope packet ← Envelope { payload = source }
mut Envelope duplicate ← copy packet
print duplicate.payload
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/field-ref-refused-reject.fab (supporting · reject)#
A field is read-only or writable per instance; ref cannot modify one.
# =============================================================================
# mutabilitas — ref is refused on fields
# =============================================================================
# A field is `let` (read-only after construction) or `mut` (reassignable).
# `ref` names a re-pointable read-only view, which a field never is.
class Punctum {
ref int x
mut int y
}
main {
print "unreachable"
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/grammar-reject.fab (supporting · reject)#
GSD-9 reject harness: use after own transfer is rejected.
# =============================================================================
# mutabilitas — grammar reject harness (MU-3)
# =============================================================================
# This exemplum intentionally fails to compile. The sibling .expected file
# pins only the stable diagnostic identity, not paths or rendered prose.
main {
let list<int> xs ← own [1]
let list<int> ys ← own xs
print xs.length()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/mut-alias-conflict.fab (supporting · reject)#
MU-4 red: two mut locals from one source conflict (SEM052).
# =============================================================================
# mutabilitas — overlapping mut locals (MU-4)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
mut list<int> a ← [1, 2, 3]
mut mut list<int> b ← a
mut mut list<int> c ← a
print b.length()
print c.length()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/mut-overlap-args.fab (supporting · reject)#
MU-4 red: the same value passed to two mut parameters conflicts.
# =============================================================================
# mutabilitas — overlapping mut arguments (MU-4)
# =============================================================================
# This exemplum intentionally fails to compile.
fn swap(mut list<int> a, mut list<int> b) → void {
a.append(1)
b.append(2)
}
main {
mut list<int> xs ← [1, 2, 3]
swap(mut xs, mut xs)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/mut-shared-conflict.fab (supporting · reject)#
MU-4 red: mut local after a live ref local of the same source is SEM052.
# =============================================================================
# mutabilitas — mut vs live ref (MU-4)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
mut list<int> a ← [1, 2, 3]
let ref list<int> r ← a
mut mut list<int> b ← a
print r.length()
print b.length()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/mut-use-while-borrowed.fab (supporting · reject)#
MU-4 red: use of a source while a mut local borrows it is SEM052.
# =============================================================================
# mutabilitas — use while mutably borrowed (MU-4)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
mut list<int> a ← [1, 2, 3]
mut mut list<int> b ← a
print a.length()
print b.length()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/mut-write-while-borrowed.fab (supporting · reject)#
MU-4 red: write of a source while a mut local borrows it is SEM052.
# =============================================================================
# mutabilitas — write while borrowed (MU-4)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
mut list<int> a ← [1, 2, 3]
mut mut list<int> b ← a
a.append(4)
print b.length()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-arg-captured-closure.fab (supporting · reject)#
A closure that captured a name before it was given away reads a dead value when called after: moved_name_captured_by_closure.
# =============================================================================
# mutabilitas — own argument: captured by closure (moved_name_captured_by_closure)
# =============================================================================
# This exemplum intentionally fails to compile.
fn eat(own list<int> xs) → void {
print xs.length()
}
main {
mut list<int> a ← [1, 2, 3]
let () → int size ← () → int ∴ a.length()
eat(own a)
print size()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-arg-element.fab (supporting · reject)#
An own argument must be a whole local binding: an element is own_argument_not_local_binding.
# =============================================================================
# mutabilitas — own argument: element (own_argument_not_local_binding)
# =============================================================================
# This exemplum intentionally fails to compile.
fn eat(own list<int> xs) → void {
print xs.length()
}
main {
mut list<list<int>> rows ← [[1], [2]]
eat(own rows[0])
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-arg-field.fab (supporting · reject)#
An own argument must be a whole local binding: a field is own_argument_not_local_binding.
# =============================================================================
# mutabilitas — own argument: field (own_argument_not_local_binding)
# =============================================================================
# This exemplum intentionally fails to compile.
class Box {
mut list<int> xs
}
fn eat(own list<int> xs) → void {
print xs.length()
}
main {
let Box b ← Box { xs = [1, 2] }
eat(own b.xs)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-arg-loop-binder.fab (supporting · reject)#
An for binder aliases a live element and cannot be given away as own: own_argument_loop_binder.
# =============================================================================
# mutabilitas — own argument: loop binder (own_argument_loop_binder)
# =============================================================================
# This exemplum intentionally fails to compile.
fn eat(own list<int> xs) → void {
print xs.length()
}
main {
let list<list<int>> rows ← [[1], [2]]
for from rows let row {
eat(own row)
}
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-arg-module-constant.fab (supporting · reject)#
A module constant cannot be given away as own: own_argument_module_constant.
# =============================================================================
# mutabilitas — own argument: module constant (own_argument_module_constant)
# =============================================================================
# This exemplum intentionally fails to compile.
fn eat(own list<int> xs) → void {
print xs.length()
}
const list<int> K = [1, 2]
main {
eat(own K)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-arg-moved-in-loop.fab (supporting · reject)#
A name declared outside a loop and given away inside its body moves on every iteration: moved_in_loop.
# =============================================================================
# mutabilitas — own argument: moved in loop (moved_in_loop)
# =============================================================================
# This exemplum intentionally fails to compile.
fn eat(own list<int> xs) → void {
print xs.length()
}
main {
mut list<int> a ← [1, 2, 3]
mut bool again ← true
while again {
eat(own a)
again ← false
}
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-arg-unmarked-param.fab (supporting · reject)#
An unmarked parameter is shared read-only and cannot be given away: own_argument_unmarked_parameter.
# =============================================================================
# mutabilitas — own argument: unmarked parameter (own_argument_unmarked_parameter)
# =============================================================================
# This exemplum intentionally fails to compile.
fn eat(own list<int> xs) → void {
print xs.length()
}
fn relay(list<int> p) → void {
eat(own p)
}
main {
relay([1, 2])
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-arg-use-after.fab (supporting · reject)#
After eat(own a) the name a is dead: a later use is use_after_own, blamed on the call.
# =============================================================================
# mutabilitas — own argument: use after own (use_after_own)
# =============================================================================
# This exemplum intentionally fails to compile.
fn eat(own list<int> xs) → void {
print xs.length()
}
main {
mut list<int> a ← [1, 2, 3]
eat(own a)
print a.length()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-from-field.fab (supporting · reject)#
MU-3 red: field projection into own rejects with copy suggestion.
# =============================================================================
# mutabilitas — field into own bind (MU-3)
# =============================================================================
# This exemplum intentionally fails to compile.
class Person {
mut list<int> friends
}
main {
let Person p ← Person { friends = [1, 2] }
let list<int> b ← own p.friends
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-from-index.fab (supporting · reject)#
MU-3 red: index projection into own rejects with copy suggestion.
# =============================================================================
# mutabilitas — index into own bind (MU-3)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
let list<list<int>> xs ← [[1], [2]]
let list<int> b ← own xs[0]
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-from-mut.fab (supporting · reject)#
MU-3 red: mut source into own rejects with copy suggestion.
# =============================================================================
# mutabilitas — mut into own bind (MU-3)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
mut mut list<int> a ← [1, 2, 3]
let list<int> b ← own a
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-from-ref.fab (supporting · reject)#
MU-3 red: ref source into own rejects with copy suggestion.
# =============================================================================
# mutabilitas — ref into own bind (MU-3)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
let ref list<int> a ← [1, 2, 3]
let list<int> b ← own a
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-from-shared.fab (supporting · reject)#
MU-3 red: branched shared RHS into own rejects with copy suggestion.
# =============================================================================
# mutabilitas — shared branch into own bind (MU-3)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
let list<int> a ← [1]
let list<int> b ← [2]
let list<int> c ← own true ✓ a ✗ b
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-from-unmarked.fab (supporting · reject)#
MU-3 red: live unmarked source into own rejects with copy suggestion.
# =============================================================================
# mutabilitas — unmarked live into own (MU-3)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
let list<int> a ← [1, 2, 3]
let list<int> b ← own a
print a.length()
print b.length()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/own-use-after-move.fab (supporting · reject)#
MU-3 red: use of an own source after bind-site transfer is SEM050.
# =============================================================================
# mutabilitas — own use after move (MU-3)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
let list<int> a ← own [1, 2, 3]
let list<int> b ← own a
print a.length()
print b.length()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/ref-assign-projection.fab (supporting · reject)#
MU-2 red: projection assignment through a ref root is rejected.
# =============================================================================
# mutabilitas — ref projection assignment (MU-2)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
let ref list<int> xs ← [1, 2, 3]
xs[0] ← 9
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/ref-genus-field.fab (supporting · reject)#
MU-2 red: mutating a referential field of a ref class is rejected.
# =============================================================================
# mutabilitas — ref class field transitivity (MU-2)
# =============================================================================
# This exemplum intentionally fails to compile.
class Person {
mut list<int> friends
}
main {
let ref Person p ← Person { friends = [1, 2] }
p.friends.append(3)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/ref-local-write-reject.fab (supporting · reject)#
A ref local reads through to a place; a write through it is refused.
# =============================================================================
# mutabilitas — no write through a ref local
# =============================================================================
# `ref Cell cur ← c` re-points freely but is read-only through. Write through
# the place's own name (`c.value ← 5`) instead.
class Cell {
mut int value
}
main {
mut Cell c ← Cell { value = 1 }
ref Cell cur ← c
cur.value ← 5
print c.value
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/ref-mutating-receiver.fab (supporting · reject)#
MU-2 red: mutating receiver intrinsic through a ref root is rejected.
# =============================================================================
# mutabilitas — ref mutating receiver (MU-2)
# =============================================================================
# This exemplum intentionally fails to compile.
main {
let ref list<int> xs ← [1, 2, 3]
xs.append(4)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/ref-param-write-reject.fab (supporting · reject)#
A bare parameter is a ref: the callee cannot write through it.
# =============================================================================
# mutabilitas — no write through a bare parameter
# =============================================================================
# A parameter with no marker is `ref`: re-pointable, read-only through. Mark it
# `mut` to write the caller's value.
class Cell {
mut int value
}
fn poke(Cell c) → none {
c.value ← 5
}
main {
mut Cell c ← Cell { value = 1 }
poke(c)
print c.value
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/ref-scalar-reject.fab (supporting · reject)#
A scalar type takes no ref marker.
# =============================================================================
# mutabilitas — no ref on a scalar
# =============================================================================
# Scalars bind by value; `ref` names a place of a shareable type.
main {
let int n ← 3
ref int m ← n
print m
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/ref-to-mut.fab (supporting · reject)#
MU-2 red: passing a ref value into a mut position is rejected.
# =============================================================================
# mutabilitas — ref into mut (MU-2)
# =============================================================================
# This exemplum intentionally fails to compile.
fn poke(mut list<int> xs) → void {
xs.append(4)
}
main {
mut ref list<int> a ← [1, 2, 3]
poke(mut a)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/ref-to-own.fab (supporting · reject)#
MU-2 red: passing a ref value into an own position is rejected.
# =============================================================================
# mutabilitas — ref into own (MU-2)
# =============================================================================
# This exemplum intentionally fails to compile.
fn take(own list<int> xs) → void {
print xs.length()
}
main {
let ref list<int> a ← [1, 2, 3]
take(own a)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/mutabilitas/var-retired-reject.fab (supporting · reject)#
The writable-name declaration word is mut; the old var spelling is retired.
# =============================================================================
# mutabilitas — var is retired
# =============================================================================
# The writable run-time name is `mut`: `mut int x ← 5`.
main {
var int x ← 5
print x
}Expected: compilation rejects this example.