12. inout parameters
An inout parameter lets a function change its caller’s variable. The
argument is written &x (&x[lo..hi] for an inout slice, below), so a
reader of the call can see it: x (or a field or element of it) must be
a var or an inout parameter. Besides arguments, & may only start a
local borrow (below), or, in an allow unsafe file, take an address where
a pointer is expected (section 18); anywhere else it is E121. Inside the function, the
parameter reads and assigns like a local.
Exclusivity (E117): a place passed as inout may not appear in any
other argument of the same call, so a function never sees two views of the
same memory. Places are compared by their fields: &v.data and v.len
are separate, &v and v.len are not. A method’s inout self is looser,
since the other arguments are computed before the call: they may read
plain values of it (l.push(a, l.len), m.mix(m.last)), which the method
sees as they were before the call, but not take a slice of it. And in any
call, a place whose owned value is moved in (to a sink parameter) may
appear in no other argument, whatever that argument’s mode: the callee
could free it while the other still sees it. Nor may the bounds of a
slice change what is sliced: b[i][0..grow(a, &b)] would move b[i]
while slicing it.
// expect: 3 1
uses io
const Mixer = struct {
state: [8]u64
last: [8]u64
fn mix(inout self: Mixer, x: [8]u64) void {
self.last = self.state // x is still the old last
self.state[0] = x[0]
}
}
fn main(io: Io) void {
var m = Mixer{ .state = @splat(3), .last = @splat(1) }
m.mix(m.last)
io.print(m.last[0], " ", m.state[0])
}
// expect: 21
uses io
fn swap(inout a: i64, inout b: i64) void {
const t = a
a = b
b = t
}
fn main(io: Io) void {
var x = 1
var y = 2
swap(&x, &y)
io.print(x * 10 + y)
}
// error: E117
uses io
fn add_into(inout acc: i64, x: i64) void {
acc = acc + x
}
fn main(io: Io) void {
var total = 1
add_into(&total, total)
io.print(total)
}
Mutable slices. An inout xs: []T parameter is a writable view: the
argument is written &x[lo..hi] (or &x[lo..], never just &x; E103),
where x is a var array, a var or inout Buf, or another inout
slice. The function writes elements (xs[i] = v, &xs[i] as an inout
argument) and may lend a part of it on (&xs[1..]), but cannot reassign
xs itself or resize it (E105); like every slice it cannot outlive the
call. Elements that own buffers or are owned structs cannot be assigned
(E139), since that would lose them, but @swap, @replace and @take
work (section 21). Exclusivity is the same as for &x: f(&b[0..2], &b[2..]) is
E117, as is f(&b[0..], b[0..]), since the callee would see the same
memory twice. Changing elements is allowed inside a for loop over x.
// expect: 0
// expect: 7
// expect: 7
// expect: 0
uses io
fn fill(inout xs: []i64, v: i64) void {
for (xs) |_, i| {
xs[i] = v
}
}
fn main(io: Io) void {
var a = [4]i64{ 0, 0, 0, 0 }
fill(&a[1..3], 7)
for (a) |x| {
io.print(x)
}
}
// error: E105
uses io
fn fill(inout xs: []i64, v: i64) void {
xs[0] = v
}
fn main(io: Io) void {
const a = [2]i64{ 1, 2 }
fill(&a[0..], 0)
io.print(a[0])
}
Local borrows. const r = &p names the place p (a variable, or a
field or element of one) without copying or moving it: r reads as p
does and may be passed to plain parameters, but cannot change it. inout
r = &p, of a place that could be passed as &p, may also change it:
assign it or its fields, pass &r or call its inout self methods
(E111 if it never does; E105 for writing through a const borrow; a
var borrow is E121). Like a local slice, a borrow lasts until its last
use: while it is used later, p cannot be moved, assigned or borrowed as
writable (E149), and while a writable one is, p cannot even be read
except through it. A borrow never owns what it names, so moving it, or any
part of it, is E139: @replace(&r.f, v) swaps a value in instead, and a
switch over a borrowed union only lends its payload. The place must
be a variable’s (&make() is E121), and a slice or str already views
its data (E116). A borrow (or a local slice) stands for what it names
everywhere: &c[i] of a local slice c borrows the container c views
(every one, for an if that views two), so changing or taking an owned
element through c or r is judged as changing that container, while a
for loop or a switch capture holds it (E137, E149).
// expect: maindb 0
// expect: maindb 11
// expect: 81
uses alloc, io
const Db = struct {
name: Buf(u8)
hits: i64
fn bump(inout self: Db) void {
self.hits = self.hits + 1
}
}
const Conf = struct {
db: Db
port: i64
}
fn show(io: Io, d: Db) void {
io.print(d.name, " ", d.hits)
}
fn main(io: Io, a: Allocator) void {
var conf = Conf{ .db = Db{ .name = a.concat("main", "db"), .hits = 0 }, .port = 80 }
defer a.free(conf)
const db = &conf.db // reads conf.db, owns nothing
show(io, db)
inout w = &conf.db // changes it
w.bump()
w.hits = w.hits + 10
show(io, conf.db) // w is not used after this
inout p = &conf.port
p = p + 1
io.print(conf.port)
}
// error: E149
uses alloc, io
const Db = struct { name: Buf(u8), hits: i64 }
fn main(io: Io, a: Allocator) void {
const db = Db{ .name = a.concat("a", "b"), .hits = 0 }
const name = &db.name
a.free(db) // name is still used below
io.print(name)
}