Reference Keywords
read
Lends a value to be looked at, not changed.
In a terminal: man sg-read. The manual page
Forms
| Form | What it is |
|---|---|
T f(read T name) |
a parameter that borrows |
f(read value) |
the call says so too |
T m(read this) |
a method that only looks |
read T[] |
a slice: items something else owns |
var b = read value |
a borrow kept in a variable |
read T f(read U from) |
a borrow given back |
What it means
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 read borrow instead: the caller keeps the value, and nothing can be changed through the
borrow.
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.
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.
Examples
Look at a value
void greet(read string name) {
print($"Hello, {name}")
}
void main() {
var name = "Pasha".clone()
greet(read name)
print(name)
}
Hello, Pasha
Pasha
main still owns the text after the call, so it can print it.
A method that only looks
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())
}
3
read this says the method changes nothing; ref this says it does.
A slice: a list, an array, or a part of one
int sum(read int[] numbers) {
var total = 0
for n in numbers {
total += n
}
return total
}
void main() {
var scores = List<int> { 3, 5, 8, 13 }
print(sum(read scores))
print(sum(scores[1..3]))
}
29
13
read T[] looks at items that something else owns. read scores lends the whole list, and scores[1..3] lends
the items at 1 and 2.
A borrow kept in a variable
void main() {
var words = List<string> { "owner".clone(), "borrow".clone() }
var first = read words[0]
print(first)
words.add("later".clone())
print(words.length())
}
owner
3
The borrow ends at the last use of first, not at the end of the block, so the list may grow after it.
A borrow given back
read string longest(read List<string> 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<string> { "owner".clone(), "borrow".clone() }
var best = longest(read words)
print(best)
}
borrow
A function may return a borrow of what it was lent. best then borrows from words, as if main had taken it
itself.
What the compiler refuses
Changing a value through the borrow
void greet(read string name) {
name.write("!")
}
void main() {
var name = "Pasha".clone()
greet(read name)
}
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`
Changing what a borrow still in use came from
void main() {
var words = List<string> { "owner".clone(), "borrow".clone() }
var first = read words[0]
words.clear()
print(first)
}
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
clear would free the text that first points to.
A borrow that outlives its value
void main() {
var name = "nobody".clone()
var shown = read name
if name.length() > 3 {
var guest = "Pasha".clone()
shown = read guest
}
print(shown)
}
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
See also
refLends a value to be changed in place; the only borrow of it while it lasts.sharedshared T: a value on the heap with several owners, only read.checkedshared checked T: a shared value changed through any owner, each borrow of it checked while the program runs.ListA list of items that grows, kept in one block of memory.- In the book

