.\" Made by website/plain/build.mjs from website/words/readonly.md; change that file. .TH SG-READONLY 7 2026-11-28 "SG" "SG reference" .SH NAME readonly \- marks a field that is given when the value is built and never changed afterwards, nor anything behind it .SH SYNOPSIS .TP \fBreadonly T field\fR a field given when the value is built, and never changed after that .TP \fBreadonly T field = value\fR the same, with a default that a literal may leave out .TP \fBpublic readonly T field\fR a field other modules can read and not write .TP \fBName { field = value }, Name(...)\fR where the field gets its value: a literal, or a constructor that builds one .TP \fBvalue with { field = x }\fR a new value, with the field given again .TP \fBvalue = Name { ... }, this = Name { ... }\fR the whole value replaced, also through ref .SH DESCRIPTION A \fBreadonly\fR field gets its value when the value it belongs to is built, by a literal or by a \fBcreate\fR that writes one, and keeps it. It is read like any other field. It cannot be assigned, with \fB=\fR or \fB+=\fR, from inside the module or outside, in a function or in a method of its own type. .PP The rule goes all the way down: nothing behind a \fBreadonly\fR field changes either. A \fBreadonly List\fR cannot be added to or sorted, a \fBreadonly string\fR cannot be written to, a field of a \fBreadonly\fR struct cannot be assigned, and an item of a \fBreadonly\fR array cannot be set. The field cannot be lent as \fBref\fR, a range of it neither, and a method that takes \fBref this\fR cannot be called on it, since any of these could change it. A \fBreadonly\fR \fBshared locked\fR or \fBshared checked\fR field is read in a \fBlock\fR block, whose name then only reads, or in \fBread ... as\fR, and is not opened with \fBref ... as\fR\&. An owner taken with \fBshare\fR is one more owner, and changes the value as every owner does: \fBreadonly\fR covers the field, not the value's other owners. \fBtake()\fR and \fBreplace\fR leave another value in the field, so they are refused too. A \fBread\fR borrow, \fB\&.clone()\fR and the methods that only look are all allowed. The owner of a value may still move a field's value out, as from any field: the value is then not used as a whole again until it is replaced. .PP The value that holds the field can still be replaced as a whole: a variable given a new value, a \fBref\fR parameter given one, \fBthis = ...\fR in a method that takes \fBref this\fR, an item of a list set again. That builds a new value, which gives the field its value again. So does \fBwith\fR, which makes a new value from an old one. A \fBCopyable\fR value copies its \fBreadonly\fR fields, and they stay \fBreadonly\fR in the copy. .PP \fBreadonly\fR is not \fBread\fR\&. \fBread\fR lends a value for a while; a \fBreadonly\fR field owns its value, for as long as the value it belongs to lives. It goes on fields of structs only: a value of the program that never changes is a \fBconst\fR or a \fBstatic\fR, and a local variable that never changes is simply not assigned again. An \fBatomic\fR field cannot be \fBreadonly\fR, since its methods change it through a \fBread\fR borrow. .PP \fBpublic readonly\fR is how a module shows a field and keeps it from being written outside. A struct whose fields are all \fBpublic\fR can still be built, and copied with \fBwith\fR, by another module, with any value in its \fBreadonly\fR fields; a field kept to the module makes it built only by the module's own functions. The check is made by the compiler and costs nothing when the program runs. .SH EXAMPLES .SS "A field given once" .RS 4 .nf struct Ticket : Formattable { readonly long number readonly string event string seat Ticket create(long number, string event) { return Ticket { number = number, event = event, seat = "standing" } } } void main() { var ticket = Ticket(1041, "Concert") ticket.seat = "B12" print(ticket.number) print(ticket) } .fi .RE .PP .RS 4 .nf 1041 Ticket { number = 1041, event = "Concert", seat = "B12" } .fi .RE .PP \fBcreate\fR gives \fBnumber\fR and \fBevent\fR their values when the ticket is built. After that, only \fBseat\fR changes. .SS "A new value in place of the old" .RS 4 .nf struct Reading : Formattable { readonly string sensor double celsius void recalibrate(ref this, string sensor) { this = Reading { sensor = sensor, celsius = this.celsius } } Reading movedTo(this, string sensor) { return this with { sensor = sensor } } } void reset(ref Reading reading) { reading = Reading { sensor = "spare", celsius = 0.0 } } void main() { var a = Reading { sensor = "kitchen", celsius = 21.5 } a.recalibrate("hall") print(a) var b = a.movedTo("garden") print(b) reset(ref b) print(b) } .fi .RE .PP .RS 4 .nf Reading { sensor = "hall", celsius = 21.5 } Reading { sensor = "garden", celsius = 21.5 } Reading { sensor = "spare", celsius = 0 } .fi .RE .PP \fBthis.sensor = sensor\fR would be refused, but \fBrecalibrate\fR builds a whole new reading and puts it in place of the old one. \fBmovedTo\fR takes the reading and gives back a new one made with \fBwith\fR\&. \fBreset\fR replaces the caller's reading through \fBref\fR\&. Each time the field gets its value where a value is built. .SS "What lies behind the field" .RS 4 .nf struct Recipe { readonly string name readonly List steps } void main() { var soup = Recipe { name = "Soup", steps = List { "chop", "boil", "stir" } } print(soup.steps.length()) for step in read soup.steps { print($"{soup.name}: {step}") } var mine = soup.steps.clone() mine.add("serve") print(mine) print(soup.steps) } .fi .RE .PP .RS 4 .nf 3 Soup: chop Soup: boil Soup: stir ["chop", "boil", "stir", "serve"] ["chop", "boil", "stir"] .fi .RE .PP The list in \fBsteps\fR is read, gone over and cloned. \fBsoup.steps.add("serve")\fR would be refused: the list itself is behind the \fBreadonly\fR field. The clone is a list of its own, and changes freely. .SS "A value that is never changed, only replaced" .RS 4 .nf struct Point : Copyable, Formattable { readonly int x readonly int y Point shifted(this, int dx) { return this with { x = this.x + dx } } } void main() { var points = List { Point { x = 1, y = 1 }, Point { x = 5, y = 2 } } for p in ref points { p = p.shifted(10) } points[0] = Point { x = 0, y = 0 } var first = points[0] print($"{first} {points}") } .fi .RE .PP .RS 4 .nf Point { x = 0, y = 0 } [Point { x = 0, y = 0 }, Point { x = 15, y = 2 }] .fi .RE .PP With every field \fBreadonly\fR, a point is never changed in place. \fBshifted\fR gets a copy, since \fBPoint\fR is \fBCopyable\fR, and gives back a new point. Through \fBref\fR, and in a list, a whole point takes the place of another; \fBp.x = 0\fR or \fBpoints[1].x = 0\fR would be refused. .SS "Read outside the module, not written" The file library/catalog/book.sg: .PP .RS 4 .nf public struct Book : Formattable { public readonly string isbn public string shelf int loans public Book create(string isbn, string shelf) { return Book { isbn = isbn, shelf = shelf, loans = 0 } } public void lend(ref this) { this.loans += 1 } } .fi .RE .PP The file library/main.sg: .PP .RS 4 .nf use catalog void main() { var book = Book("978-0441013593", "A3") book.shelf = "C1" book.lend() print(book.isbn) print(book) } .fi .RE .PP The file library/relabel.sg: .PP .RS 4 .nf use catalog void main() { var book = Book("978-0441013593", "A3") book.isbn = "978-0000000000" } .fi .RE .PP The file library/copy.sg: .PP .RS 4 .nf use catalog void main() { var book = Book("978-0441013593", "A3") var other = book with { isbn = "978-0000000000" } } .fi .RE .PP .RS 4 .nf $ cd library $ sg main.sg 978-0441013593 Book { isbn = "978-0441013593", shelf = "C1", loans = 1 } $ sg relabel.sg error: `isbn` is `readonly`: it is given when the value is built and not changed afterwards --> relabel.sg:5:10 5 | book.isbn = "978-0000000000" | ^^^^ $ sg copy.sg error: `loans` is not public, so a `Book` cannot be copied with `with` outside the `catalog` module --> copy.sg:5:17 5 | var other = book with { isbn = "978-0000000000" } | ^^^^ .fi .RE .PP \fBmain.sg\fR reads \fBisbn\fR and writes \fBshelf\fR\&. \fBrelabel.sg\fR cannot write \fBisbn\fR\&. \fBcopy.sg\fR cannot make a book with another \fBisbn\fR either: \fBloans\fR is not public, so outside \fBcatalog\fR a book is built only by \fBcreate\fR, and copied with \fBwith\fR not at all. Without such a field, \fBBook { ... }\fR and \fBwith\fR would work there. .SH "WHAT THE COMPILER REFUSES" .SS "Assigning the field" .RS 4 .nf struct Ticket { readonly long number string seat } void main() { var ticket = Ticket { number = 1041, seat = "B12" } ticket.number += 1 } .fi .RE .PP .RS 4 .nf error: `number` is `readonly`: it is given when the value is built and not changed afterwards --> example.sg:8:12 8 | ticket.number += 1 | ^^^^^^ .fi .RE .SS "Assigning it in a method of its own type" .RS 4 .nf struct Reading { readonly string sensor double celsius void rename(ref this, string sensor) { this.sensor = sensor } } void main() { var a = Reading { sensor = "kitchen", celsius = 21.5 } a.rename("hall") } .fi .RE .PP .RS 4 .nf error: `sensor` is `readonly`: it is given when the value is built and not changed afterwards --> example.sg:6:14 6 | this.sensor = sensor | ^^^^^^ .fi .RE .PP \fBthis = Reading { sensor = sensor, celsius = this.celsius }\fR replaces the whole reading, and is allowed. .SS "Changing what lies behind it" .RS 4 .nf struct Recipe { readonly string name readonly List steps } void main() { var soup = Recipe { name = "Soup", steps = List { "chop", "boil" } } soup.steps.add("serve") } .fi .RE .PP .RS 4 .nf error: `steps` is `readonly`: it is given when the value is built and not changed afterwards `add` changes the list --> example.sg:8:10 8 | soup.steps.add("serve") | ^^^^^ .fi .RE .SS "A ref this method on it" .RS 4 .nf struct Clock { int ticks void tick(ref this) { this.ticks += 1 } } struct Station { readonly Clock clock } void main() { var station = Station { clock = Clock { ticks = 0 } } station.clock.tick() } .fi .RE .PP .RS 4 .nf error: `clock` is `readonly`: it is given when the value is built and not changed afterwards `tick` takes `ref this` and may change it --> example.sg:15:13 15 | station.clock.tick() | ^^^^^ .fi .RE .SS "Lending it as ref" .RS 4 .nf struct Recipe { readonly string name } void shout(ref string text) { text.write("!") } void main() { var soup = Recipe { name = "Soup" } shout(ref soup.name) } .fi .RE .PP .RS 4 .nf error: `name` is `readonly`: it is given when the value is built and not changed afterwards so it cannot be lent as `ref` either --> example.sg:11:20 11 | shout(ref soup.name) | ^^^^ .fi .RE .SS "Taking its value" .RS 4 .nf struct Order { readonly string? coupon } void main() { var order = Order { coupon = "SPRING" } var code = order.coupon.take() print(code ?? "none") } .fi .RE .PP .RS 4 .nf error: `coupon` is `readonly`: it is given when the value is built and not changed afterwards `take` puts `null` in its place --> example.sg:7:22 7 | var code = order.coupon.take() | ^^^^^^ .fi .RE .PP \fBtake()\fR leaves \fBnull\fR in the field, which changes it. The owner of the order may move the text out, \fBvar code = order.coupon\fR; the order is then not used as a whole again until it is replaced. .SS "readonly on a local variable" .RS 4 .nf void main() { readonly int limit = 3 print(limit) } .fi .RE .PP .RS 4 .nf error: expected an expression, found `readonly` `readonly` is written on a field of a struct; a value that does not change is a `const`, or a variable that is not assigned again --> example.sg:2:5 2 | readonly int limit = 3 | ^^^^^^^^ hint: `const int limit = 3` .fi .RE .PP \fBreadonly\fR goes on a field of a struct. A local that never changes is a \fBvar\fR not assigned again, or a \fBconst int Limit = 3\fR\&. .SS "An atomic field" .RS 4 .nf struct Server { readonly atomic long requests } void main() { } .fi .RE .PP .RS 4 .nf error: an `atomic long` field is not `readonly`: its methods change it through a `read` borrow --> example.sg:2:26 2 | readonly atomic long requests | ^^^^^^^^ hint: keep the field to its module and give a method that reads it: `long requests(read this) { return this.requests.load() }` .fi .RE .SS "Changing a shared value through the field" .RS 4 .nf struct Folder { string name } struct Holder { readonly shared locked Folder folder } void main() { var h = Holder { folder = new shared locked Folder { name = "a".clone() } } lock h.folder as f { f.name = "b".clone() } } .fi .RE .PP .RS 4 .nf error: `f` only reads what `h.folder` holds, since `folder` is `readonly` `readonly` stops every change made through the field, a `lock` block's too --> example.sg:12:9 12 | f.name = "b".clone() | ^^^^^^ .fi .RE .PP \fBlock h.folder as f { print(f.name) }\fR reads it. Another owner, \fBvar owner = share h.folder\fR, may change it in a \fBlock\fR block of its own, as any owner of the value may. .SH "SEE ALSO" .BR sg-struct (7), .BR sg-public (7), .BR sg-with (7), .BR sg-ref (7), .BR sg-this (7), .BR sg-const (7) .PP https://sg\-lang.org/reference/readonly.html