.\" Made by website/plain/build.mjs from website/words/struct.md; change that file. .TH SG-STRUCT 7 2026-11-28 "SG" "SG reference" .SH NAME struct \- declares a type made of named fields, with the methods that work on them .SH SYNOPSIS .TP \fBstruct Name { T field ... }\fR a type with one field on each line .TP \fBT field = value\fR a field with a default value .TP \fBreadonly T field, public T field\fR a field given only when the value is built; a field other modules see .TP \fBstruct Name : Copyable, Formattable\fR a type that takes built-in interfaces .TP \fBName { field = value, ... }\fR a value, every field given or left to its default .TP \fBvalue.field\fR a field, read or written .TP \fBT m(read this), ref this, this\fR a method that looks, changes, or takes the value .TP \fBName create(...), Name(...)\fR a constructor, and the short way to call it .TP \fBvalue with { field = value }\fR a copy with some fields changed .TP \fBpacked struct, align(N) struct\fR a struct with no padding; one kept at a multiple of N bytes .SH DESCRIPTION A struct puts several values together under one name. Each field is written type first, one per line. A value is built by naming its fields, in any order: \fBBook { title = "Dune", pages = 412 }\fR\&. Every field gets a value when the value is built. A field with a default may be left out; any other field must be given. In the head of an \fBif\fR, \fBwhile\fR or \fBfor\fR, where \fB{\fR starts the block, a literal goes in parentheses: \fBif p == (Point { x = 1, y = 2 }) {\fR\&. .PP Methods are written inside the struct. The first parameter says what a method does with the value it is called on: \fBread this\fR only looks, \fBref this\fR may change it, and \fBthis\fR takes it, so the caller's variable is used up. A function without \fBthis\fR belongs to the type and is called on it, \fBBook.create(...)\fR; one named \fBcreate\fR is the constructor, and \fBBook(...)\fR is short for calling it. A field is always reached through \fBthis.\fR, so a field and a local variable never look alike. .PP A struct value lives where its variable is, with no allocation of its own. It has one owner: giving it to a variable, a field or a parameter moves it, and the old name can no longer be used. A struct marked \fBCopyable\fR, whose fields are all numbers, \fBbool\fR, \fBchar\fR or other \fBCopyable\fR types, is copied instead. Nothing else is there unless the struct asks for it after the \fB:\fR: \fBFormattable\fR to be printed, \fBEquatable\fR for \fB==\fR, \fBClonable\fR for \fB\&.clone()\fR, and the others in the book. .PP The fields lie in the order they are written, as C lays them out, so the largest first leaves the fewest gaps between them. \fBpacked struct\fR and \fBalign(N) struct\fR are for a layout that a file, a device or C expects. .SH EXAMPLES .SS "Fields and a literal" .RS 4 .nf struct Book : Formattable { string title int pages bool lent = false } void main() { var book = Book { title = "Dune", pages = 412 } book.pages += 2 print(book.title) print(book) } .fi .RE .PP .RS 4 .nf Dune Book { title = "Dune", pages = 414, lent = false } .fi .RE .PP \fBlent\fR is left out, so it gets its default. \fB: Formattable\fR lets \fBprint\fR write the whole value, the way it is built. .SS "Methods and a constructor" .RS 4 .nf struct Cart { List items long total Cart create() { return Cart { items = List(), total = 0 } } void add(ref this, string item, long price) { this.items.add(item) this.total += price } nint count(read this) { return this.items.length() } string receipt(this) { return $"{this.count()} items, {this.total} cents".toString() } } void main() { var cart = Cart() cart.add("tea", 450) cart.add("cups", 1200) print(cart.count()) var text = cart.receipt() print(text) } .fi .RE .PP .RS 4 .nf 2 2 items, 1650 cents .fi .RE .PP \fBCart()\fR calls \fBcreate\fR\&. \fBadd\fR changes the cart and \fBcount\fR only looks; the call does not write \fBref\fR or \fBread\fR for the value before the dot. \fBreceipt\fR takes the cart, so \fBcart\fR cannot be used after that line. .SS "A readonly field" .RS 4 .nf struct Reading : Formattable { readonly string sensor double celsius } void main() { var last = Reading { sensor = "kitchen", celsius = 21.5 } last.celsius = 22.0 print(last) last = Reading { sensor = "garden", celsius = 9.5 } print(last.sensor) } .fi .RE .PP .RS 4 .nf Reading { sensor = "kitchen", celsius = 22 } garden .fi .RE .PP \fBsensor\fR is given when the value is built and cannot be assigned after that, nor changed through a method. The variable that holds the value can still be given a whole new one. .SS "A copy with with" .RS 4 .nf struct Point : Copyable, Formattable, Equatable { int x int y Point origin() { return Point { x = 0, y = 0 } } Point moved(this, int dx) { return this with { x = this.x + dx } } } void main() { var p = Point { x = 1, y = 2 } var q = p.moved(4) print($"{p} {q}") print(q with { x = 0, y = 0 } == Point.origin()) } .fi .RE .PP .RS 4 .nf Point { x = 1, y = 2 } Point { x = 5, y = 2 } true .fi .RE .PP \fBwith\fR makes a new value with the fields written changed and the others as they were. \fBPoint\fR is \fBCopyable\fR, so \fBmoved\fR gets a copy and \fBp\fR is still there. On a struct that is not \fBCopyable\fR, \fBwith\fR and \fBthis\fR move the value. \fBorigin\fR has no \fBthis\fR and is called on the type. .SS "A struct that holds a list" .RS 4 .nf struct Playlist { string name List songs } nint totalLength(read Playlist list) { nint sum = 0 for song in read list.songs { sum += song.length() } return sum } void main() { var mix = Playlist { name = "Morning", songs = List { "Intro", "Sunrise" } } print(totalLength(read mix)) var songs = mix.songs print(mix.name) print(songs) mix.songs = List { "Encore" } print(mix.songs) } .fi .RE .PP .RS 4 .nf 12 Morning ["Intro", "Sunrise"] ["Encore"] .fi .RE .PP \fBread mix\fR lends the playlist, list and all, without copying. \fBvar songs = mix.songs\fR moves the list out of the field: \fBmix.name\fR can still be used, but \fBmix\fR as a whole cannot until \fBmix.songs\fR has a value again. .SS "Layout: packed and align" .RS 4 .nf struct Plain { byte kind uint length } packed struct Header { byte kind uint length } align(64) struct Slot { long count } void main() { print(Memory.sizeOf()) print(Memory.sizeOf
()) print(Memory.sizeOf()) var header = Header { kind = 1, length = 300 } print(header.length) } .fi .RE .PP .RS 4 .nf 8 5 64 300 .fi .RE .PP \fBPlain\fR takes eight bytes: \fBlength\fR must lie at a multiple of four, so three stay empty. \fBHeader\fR has no gap, as a format on the disk may want; its fields are copied out, not lent. \fBSlot\fR starts at a multiple of 64 bytes wherever it is kept, the heap included. .SH "WHAT THE COMPILER REFUSES" .SS "A field left out" .RS 4 .nf struct Book { string title int pages bool lent = false } void main() { var book = Book { title = "Dune" } print(book.title) } .fi .RE .PP .RS 4 .nf error: `Book` is missing `pages` every field gets a value; there is no uninitialized memory --> example.sg:8:16 8 | var book = Book { title = "Dune" } | ^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP \fBlent\fR may be left out, since it has a default; \fBpages\fR has none. .SS "Changing a readonly field" .RS 4 .nf struct Reading { readonly string sensor double celsius } void main() { var last = Reading { sensor = "kitchen", celsius = 21.5 } last.sensor = "garden" print(last.celsius) } .fi .RE .PP .RS 4 .nf error: `sensor` is `readonly`: it is given when the value is built and not changed afterwards --> example.sg:8:10 8 | last.sensor = "garden" | ^^^^^^ .fi .RE .SS "A struct used after it was moved" .RS 4 .nf struct Playlist { string name List songs } void play(Playlist list) { print(list.name) } void main() { var mix = Playlist { name = "Morning", songs = List { "Intro" } } play(mix) print(mix.songs.length()) } .fi .RE .PP .RS 4 .nf error: `mix` was moved and can no longer be used --> example.sg:13:11 13 | print(mix.songs.length()) | ^^^ it was moved here: --> example.sg:12:10 12 | play(mix) | ^^^ .fi .RE .PP \fBplay\fR takes the playlist and lets it go when it ends. To keep it, \fBplay\fR takes \fBread Playlist\fR and the call writes \fBplay(read mix)\fR\&. .SS "A struct with a field moved out" .RS 4 .nf struct Playlist { string name List songs } void main() { var mix = Playlist { name = "Morning", songs = List { "Intro" } } var songs = mix.songs print(songs) var copy = mix print(copy.name) } .fi .RE .PP .RS 4 .nf error: `mix` cannot be used as a whole: `mix.songs` was moved out of it --> example.sg:10:16 10 | var copy = mix | ^^^ it was moved here: --> example.sg:8:17 8 | var songs = mix.songs | ^^^^^^^^^ hint: give `mix.songs` a value again, or use the other fields one by one .fi .RE .SH "SEE ALSO" .BR sg-with (7), .BR sg-readonly (7), .BR sg-this (7), .BR sg-enum (7), .BR sg-interface (7), .BR sg-packed (7) .PP https://sg\-lang.org/reference/struct.html