.\" Made by website/plain/build.mjs from website/words/read.md; change that file. .TH SG-READ 7 2026-11-28 "SG" "SG reference" .SH NAME read \- lends a value to be looked at, not changed .SH SYNOPSIS .TP \fBT f(read T name)\fR a parameter that borrows .TP \fBf(read value)\fR the call says so too .TP \fBT m(read this)\fR a method that only looks .TP \fBread T[]\fR a slice: items something else owns .TP \fBvar b = read value\fR a borrow kept in a variable .TP \fBread T f(read U from)\fR a borrow given back .SH DESCRIPTION Every value in SG has one owner, and giving a value to a function moves it there. A function that only needs to look takes a \fBread\fR borrow instead: the caller keeps the value, and nothing can be changed through the borrow. .PP The word is written twice, at the parameter and at the call, so you see at the call what it may do to your variable. .PP A borrow given to a call lasts for the call. A borrow kept in a variable lasts until the last use of that variable. While it lasts, what it borrows from cannot be changed or moved: the compiler refuses a program that tries. A plain borrow costs nothing while the program runs. .SH EXAMPLES .SS "Look at a value" .RS 4 .nf void greet(read string name) { print($"Hello, {name}") } void main() { var name = "Pasha".clone() greet(read name) print(name) } .fi .RE .PP .RS 4 .nf Hello, Pasha Pasha .fi .RE .PP \fBmain\fR still owns the text after the call, so it can print it. .SS "A method that only looks" .RS 4 .nf struct Counter { int count int value(read this) { return this.count } void add(ref this, int amount) { this.count += amount } } void main() { var hits = Counter { count = 1 } hits.add(2) print(hits.value()) } .fi .RE .PP .RS 4 .nf 3 .fi .RE .PP \fBread this\fR says the method changes nothing; \fBref this\fR says it does. .SS "A slice: a list, an array, or a part of one" .RS 4 .nf int sum(read int[] numbers) { var total = 0 for n in numbers { total += n } return total } void main() { var scores = List { 3, 5, 8, 13 } print(sum(read scores)) print(sum(scores[1..3])) } .fi .RE .PP .RS 4 .nf 29 13 .fi .RE .PP \fBread T[]\fR looks at items that something else owns. \fBread scores\fR lends the whole list, and \fBscores[1..3]\fR lends the items at 1 and 2. .SS "A borrow kept in a variable" .RS 4 .nf void main() { var words = List { "owner".clone(), "borrow".clone() } var first = read words[0] print(first) words.add("later".clone()) print(words.length()) } .fi .RE .PP .RS 4 .nf owner 3 .fi .RE .PP The borrow ends at the last use of \fBfirst\fR, not at the end of the block, so the list may grow after it. .SS "A borrow given back" .RS 4 .nf read string longest(read List words) { nint at = 0 for i in 0..words.length() { if words[i].length() > words[at].length() { at = i } } return read words[at] } void main() { var words = List { "owner".clone(), "borrow".clone() } var best = longest(read words) print(best) } .fi .RE .PP .RS 4 .nf borrow .fi .RE .PP A function may return a borrow of what it was lent. \fBbest\fR then borrows from \fBwords\fR, as if \fBmain\fR had taken it itself. .SH "WHAT THE COMPILER REFUSES" .SS "Changing a value through the borrow" .RS 4 .nf void greet(read string name) { name.write("!") } void main() { var name = "Pasha".clone() greet(read name) } .fi .RE .PP .RS 4 .nf error: `write` changes the text, but it is reached through a `read` borrow --> example.sg:2:5 2 | name.write("!") | ^^^^ hint: take the text as `ref string` instead of `read string` .fi .RE .SS "Changing what a borrow still in use came from" .RS 4 .nf void main() { var words = List { "owner".clone(), "borrow".clone() } var first = read words[0] words.clear() print(first) } .fi .RE .PP .RS 4 .nf error: `words` cannot be changed here: `first` borrows from it and is used again later --> example.sg:4:5 4 | words.clear() | ^^^^^ `first` borrowed it here: --> example.sg:3:17 3 | var first = read words[0] | ^^^^^^^^^^^^^ hint: finish with `first` before this line: a borrow ends at its last use .fi .RE .PP \fBclear\fR would free the text that \fBfirst\fR points to. .SS "A borrow that outlives its value" .RS 4 .nf void main() { var name = "nobody".clone() var shown = read name if name.length() > 3 { var guest = "Pasha".clone() shown = read guest } print(shown) } .fi .RE .PP .RS 4 .nf error: `shown` borrows from `guest` here, but `guest` is gone at the end of its block and `shown` is used after that --> example.sg:6:17 6 | shown = read guest | ^^^^^^^^^^ hint: declare `guest` where `shown` is declared, so that it lives as long .fi .RE .SH "SEE ALSO" .BR sg-ref (7), .BR sg-shared (7), .BR sg-checked (7), .BR sg-list (7) .PP https://sg\-lang.org/reference/read.html