.\" Made by website/plain/build.mjs from website/words/ref.md; change that file. .TH SG-REF 7 2026-11-28 "SG" "SG reference" .SH NAME ref \- lends a value to be changed in place; the only borrow of it while it lasts .SH SYNOPSIS .TP \fBvoid f(ref T name)\fR a parameter that changes what it is lent .TP \fBf(ref value)\fR the call says so too .TP \fBvoid m(ref this)\fR a method that changes its value .TP \fBref T[]\fR a slice whose items may be written .TP \fBvar b = ref value\fR a borrow kept in a variable .TP \fBref xs[..half], ref xs[half..]\fR two parts of one list, lent at once .SH DESCRIPTION A \fBref\fR borrow lends a value to be changed where it lies: nothing is copied, and the caller sees the change after the call. As with \fBread\fR, the word is written at the parameter and at the call, so a call that may change your variable says so. .PP A \fBref\fR borrow must be the only borrow of its value while it lasts. Two names that change the same place at once are how a function ends up reading what it has just overwritten; SG refuses that when the program compiles. The borrow ends at the last use of the name that holds it. .SH EXAMPLES .SS "Change a number" .RS 4 .nf void addOne(ref int count) { count += 1 } void main() { var count = 41 addOne(ref count) print(count) } .fi .RE .PP .RS 4 .nf 42 .fi .RE .SS "Change the items of an array or a list" .RS 4 .nf void scale(ref int[] values, int by) { for i in 0..values.length() { values[i] *= by } } void main() { var samples = int[4] { 3, 9, 4, 1 } scale(ref samples, 2) print(samples) var more = List { 1, 2, 3 } scale(ref more, 10) print(more) } .fi .RE .PP .RS 4 .nf [6, 18, 8, 2] [10, 20, 30] .fi .RE .PP \fBref T[]\fR takes an array and a list alike: it is a window on items that something else owns. .SS "One item, lent" .RS 4 .nf void main() { var scores = List { 10, 20, 30 } var last = ref scores[^1] last += 5 print(scores) } .fi .RE .PP .RS 4 .nf [10, 20, 35] .fi .RE .PP \fBlast\fR is the last item itself, not a copy of it. .SS "Two parts of one list at once" .RS 4 .nf void swap(ref int a, ref int b) { var kept = a a = b b = kept } void main() { var xs = List { 1, 2, 3, 10, 20, 30 } var half = xs.length() / 2 var left = ref xs[..half] var right = ref xs[half..] for i in 0..half { swap(ref left[i], ref right[i]) } print(xs) } .fi .RE .PP .RS 4 .nf [10, 20, 30, 1, 2, 3] .fi .RE .PP Two windows that share no item are two places, so both may be lent for changing. Whether they overlap is checked when the second is taken: where the ends are numbers, when the program compiles; otherwise when it runs, as an index out of range is. .SH "WHAT THE COMPILER REFUSES" .SS "A call that does not say ref" .RS 4 .nf void scale(ref int[] values, int by) { for i in 0..values.length() { values[i] *= by } } void main() { var samples = int[4] { 3, 9, 4, 1 } scale(samples, 2) } .fi .RE .PP .RS 4 .nf error: expected `ref int[]`, found `int[4]` --> example.sg:9:11 9 | scale(samples, 2) | ^^^^^^^ hint: lend it with `ref`: `ref samples` .fi .RE .SS "The same value lent twice" .RS 4 .nf void addTo(ref int total, ref int amount) { total += amount amount = 0 } void main() { var balance = 10 addTo(ref balance, ref balance) print(balance) } .fi .RE .PP .RS 4 .nf error: `balance` is lent twice in one call, and a `ref` borrow must be the only one --> example.sg:8:28 8 | addTo(ref balance, ref balance) | ^^^^^^^ the other borrow is here: --> example.sg:8:15 8 | addTo(ref balance, ref balance) | ^^^^^^^ hint: copy what is needed into a variable before the call .fi .RE .PP With both names on one place, \fBamount = 0\fR would also set \fBtotal\fR to 0. .SS "Reading a value while a ref borrow of it is in use" .RS 4 .nf void main() { var count = 1 var other = ref count print(count) other += 1 } .fi .RE .PP .RS 4 .nf error: `count` cannot be used here: `other` holds a `ref` borrow of it and is used again later --> example.sg:4:11 4 | print(count) | ^^^^^ `other` borrowed it here: --> example.sg:3:17 3 | var other = ref count | ^^^^^^^^^ hint: finish with `other` before this line: a borrow ends at its last use .fi .RE .SH "SEE ALSO" .BR sg-read (7), .BR sg-shared (7), .BR sg-checked (7), .BR sg-list (7) .PP https://sg\-lang.org/reference/ref.html