.\" Made by website/plain/build.mjs from website/words/with.md; change that file. .TH SG-WITH 7 2026-11-28 "SG" "SG reference" .SH NAME with \- a copy of a struct with some fields changed: p with { x = 3 } .SH SYNOPSIS .TP \fBp with { x = 3 }\fR a new value of p's struct, with x changed and the other fields as they were .TP \fBp with { x = 3, y = 4 }\fR several fields changed, in any order .TP \fBp.clone() with { ... }\fR a copy of a value that is kept, or only lent .TP \fBthis with { ... }\fR in a method that takes this, the value it was called on, changed .TP \fBb with { corner = b.corner with { x = 0 } }\fR a field changed inside a field .TP \fBif (p with { ... }) == q {\fR in the head of if, while or for, in parentheses .SH DESCRIPTION \fBwith\fR makes a new value of a struct from another one. The fields written in the braces get the values written there; every other field is taken as it was. It is C#'s \fBwith\fR on a record, and Rust's \fBPoint { x: 3, ..p }\fR\&. .PP The value on the left is taken, as \fBvar q = p\fR takes it. A \fBCopyable\fR struct is copied, so \fBp\fR is still there after. Any other struct is moved into the new value, and its old name cannot be used again: nothing is copied that is not written. To keep the old value, clone it where the copy is made: \fBp.clone() with { ... }\fR\&. A value that is only lent, such as a \fBread\fR parameter, is taken only when it is \fBCopyable\fR\&. .PP The left side runs first, then the values in the braces in the order written, and only then are the old values of the changed fields dropped; a \fBDisposable\fR one is disposed of there. A value in the braces cannot read a struct that the left side has moved, so what it needs is read into a variable first. .PP Compiled, \fBwith\fR costs what moving the struct costs: its bytes are moved or copied once and the fields written are set, with no allocation. A text or a list in a field that is not written moves with the struct and is not copied. .PP \fBwith\fR builds a value as a literal does and keeps the literal's rules. Every field written must exist, once, with a value of its type. A \fBreadonly\fR field may be written, since the result is a new value. In another module, \fBwith\fR copies a struct only when that module sees all of its fields. It works on a struct, not on an enum, a number, a \fBT*\fR or a \fBshared T\fR\&. .PP \fBwith\fR binds tighter than an operator between two values and looser than one in front: \fBa == b with { ... }\fR compares \fBa\fR with the changed \fBb\fR, and \fBtry load() with { ... }\fR changes what \fBtry\fR gives. In the head of an \fBif\fR, \fBwhile\fR or \fBfor\fR, where \fB{\fR starts the block, it goes in parentheses, as a literal does. .SH EXAMPLES .SS "A copy with a field changed" .RS 4 .nf struct Point : Copyable, Formattable { int x int y } struct Label : Formattable { string text int size bool bold = false } void main() { var p = Point { x = 1, y = 2 } var q = p with { x = 5 } print($"{p} {q}") var title = Label { text = "Report", size = 12 } var big = title with { size = 24, bold = true } print(big) } .fi .RE .PP .RS 4 .nf Point { x = 1, y = 2 } Point { x = 5, y = 2 } Label { text = "Report", size = 24, bold = true } .fi .RE .PP \fBPoint\fR is \fBCopyable\fR, so \fBp\fR is copied and still there. \fBLabel\fR holds a \fBstring\fR and is not: \fBtitle\fR is moved into \fBbig\fR, its text with it, and is not used again. .SS "Keeping the old value" .RS 4 .nf struct Label : Formattable, Clonable { string text int size } void main() { var title = Label { text = "Report", size = 12 } var big = title.clone() with { size = 24 } var draft = title with { text = "Draft" } print(big) print(draft) } .fi .RE .PP .RS 4 .nf Label { text = "Report", size = 24 } Label { text = "Draft", size = 12 } .fi .RE .PP \fBtitle.clone()\fR is a copy with a text of its own, and \fBwith\fR takes that. The last use of \fBtitle\fR moves it, and its old text is dropped where \fB"Draft"\fR takes its place. .SS "In a method" .RS 4 .nf struct Settings : Formattable { string host int port = 80 bool secure = false Settings secured(this) { return this with { port = 443, secure = true } } } void main() { var plain = Settings { host = "example.org" } var safe = plain.secured() print(safe) } .fi .RE .PP .RS 4 .nf Settings { host = "example.org", port = 443, secure = true } .fi .RE .PP \fBsecured\fR takes \fBthis\fR, so it owns the settings and gives them back changed. \fBplain\fR is used up by the call. .SS "A lent value" .RS 4 .nf struct Point : Copyable, Formattable { int x int y } Point shifted(read Point p) { return p with { x = p.x + 1 } } void main() { var p = Point { x = 1, y = 2 } print(shifted(read p)) print(p) } .fi .RE .PP .RS 4 .nf Point { x = 2, y = 2 } Point { x = 1, y = 2 } .fi .RE .PP \fBp\fR is only lent to \fBshifted\fR, and \fBwith\fR copies it, since \fBPoint\fR is \fBCopyable\fR\&. As the left side is copied, the braces may still read \fBp\fR\&. .SS "A field inside a field" .RS 4 .nf struct Point : Copyable, Formattable, Equatable { int x int y } struct Box : Copyable, Formattable, Equatable { Point corner int width int height } void main() { var box = Box { corner = Point { x = 0, y = 0 }, width = 10, height = 5 } var moved = box with { corner = box.corner with { x = 3 } } print(moved) print((moved with { corner = box.corner }) == box) print(moved with { width = 2 } == box) } .fi .RE .PP .RS 4 .nf Box { corner = Point { x = 3, y = 0 }, width = 10, height = 5 } true false .fi .RE .PP The inner \fBwith\fR makes the new corner, the outer one the new box. In the last line \fBwith\fR binds tighter than \fB==\fR, so no parentheses are needed: the box with a width of 2 is compared with \fBbox\fR\&. .SS "The old value of a field is dropped" .RS 4 .nf struct LogFile : Disposable { string path void dispose(ref this) { print($"close {this.path}") } } struct Job { string name LogFile log } void main() { var job = Job { name = "build", log = LogFile { path = "a.log" } } print("start") var next = job with { log = LogFile { path = "b.log" } } print($"now {next.name} writes {next.log.path}") } .fi .RE .PP .RS 4 .nf start close a.log now build writes b.log close b.log .fi .RE .PP The \fBwith\fR line takes \fBjob\fR, puts the new log in, and disposes of the old one there. \fBname\fR moves to \fBnext\fR unchanged. .SH "WHAT THE COMPILER REFUSES" .SS "A field the struct does not have" .RS 4 .nf struct Label { string text int size } void main() { var title = Label { text = "Report", size = 12 } var big = title with { sise = 24 } print(big.size) } .fi .RE .PP .RS 4 .nf error: `Label` has no field `sise` --> example.sg:8:28 8 | var big = title with { sise = 24 } | ^^^^ hint: `size` .fi .RE .SS "A field written twice" .RS 4 .nf struct Point : Copyable { int x int y } void main() { var p = Point { x = 1, y = 2 } var q = p with { x = 3, x = 4 } print(q.x) } .fi .RE .PP .RS 4 .nf error: field `x` is given twice --> example.sg:8:29 8 | var q = p with { x = 3, x = 4 } | ^ .fi .RE .SS "Nothing changed" .RS 4 .nf struct Point : Copyable { int x int y } void main() { var p = Point { x = 1, y = 2 } var q = p with { } print(q.x) } .fi .RE .PP .RS 4 .nf error: `with { }` changes nothing --> example.sg:8:13 8 | var q = p with { } | ^^^^^^^^^^ hint: write at least one field: `p with { x = 3 }` .fi .RE .PP A copy of a \fBCopyable\fR value is \fBvar q = p\fR; of another one, \fBp.clone()\fR\&. .SS "A value that is not a struct" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) } void main() { var s = Shape.Rect(3, 4) var wide = s with { width = 5 } print(wide is Rect) } .fi .RE .PP .RS 4 .nf error: `with` makes a new value of a struct from another, and this is a `Shape` a struct is copied with its fields changed: `p with { x = 3 }` --> example.sg:8:16 8 | var wide = s with { width = 5 } | ^ .fi .RE .PP A variant is built again instead: \fBShape.Rect(5, 4)\fR\&. .SS "A T* or a shared value" .RS 4 .nf struct Point : Copyable, Formattable { int x int y } void main() { var origin = new shared Point { x = 1, y = 2 } var moved = origin with { x = 20 } print(moved) } .fi .RE .PP .RS 4 .nf error: `with` makes a new value of a struct from another, and this is a `shared Point` the other owners of a `shared` value still look at it; `with` takes a copy of it, inside a block that opens it --> example.sg:8:17 8 | var moved = origin with { x = 20 } | ^^^^^^ hint: `read origin as point { ... point with { ... } ... }` .fi .RE .PP \fBwith\fR takes a struct. What a \fBshared\fR value holds stays with its other owners, and what a \fBT*\fR points at with its owner, so \fBwith\fR takes a copy: inside \fBread origin as point { }\fR, \fBpoint with { x = 20 }\fR; for a \fBT*\fR, \fBlabel.clone() with { ... }\fR\&. .SS "The old value used after it was moved" .RS 4 .nf struct Label { string text int size } void main() { var title = Label { text = "Report", size = 12 } var big = title with { size = 24 } print(title.text) print(big.size) } .fi .RE .PP .RS 4 .nf error: `title` was moved and can no longer be used --> example.sg:9:11 9 | print(title.text) | ^^^^^ it was moved here: --> example.sg:8:15 8 | var big = title with { size = 24 } | ^^^^^ .fi .RE .PP \fBtitle.clone() with { ... }\fR keeps \fBtitle\fR, when \fBLabel\fR is \fB: Clonable\fR\&. .SS "The moved value read in the braces" .RS 4 .nf struct Label { string text int size } void main() { var title = Label { text = "Report", size = 12 } var big = title with { size = title.size * 2 } print(big.size) } .fi .RE .PP .RS 4 .nf error: `with` uses `title` up before the new values are worked out, and the value of `size` reads it --> example.sg:8:35 8 | var big = title with { size = title.size * 2 } | ^^^^^ hint: read it into a variable first: `var size = title.size * 2`, then `title with { size = size }` .fi .RE .PP The left side runs first and moves \fBtitle\fR\&. \fBvar size = title.size * 2\fR on the line before, and \fBtitle with { size = size }\fR, make the program right. .SS "A lent value that is not Copyable" .RS 4 .nf struct Label { string text int size } Label bigger(read Label label) { return label with { size = 24 } } void main() { var title = Label { text = "Report", size = 12 } print(bigger(read title).size) } .fi .RE .PP .RS 4 .nf error: `with` takes the value of `label`, which is lent here, and a `Label` is not `Copyable` --> example.sg:7:12 7 | return label with { size = 24 } | ^^^^^ .fi .RE .PP \fBwith\fR would move the text out of a label the caller still owns. With \fB: Clonable\fR on \fBLabel\fR, \fBlabel.clone() with { ... }\fR makes a copy first. .SS "Without parentheses in an if" .RS 4 .nf struct Point : Copyable, Equatable { int x int y } void main() { var p = Point { x = 1, y = 2 } if p with { x = 1 } == p { print("same") } } .fi .RE .PP .RS 4 .nf error: `with` needs parentheses here, where `{` starts the block --> example.sg:8:10 8 | if p with { x = 1 } == p { | ^^^^ hint: `(p with { ... })` .fi .RE .SS "A field another module does not see" The file bank/accounts/account.sg: .PP .RS 4 .nf public struct Account { public readonly string owner public string? note long cents public Account create(string owner) { return Account { owner = owner, note = null, cents = 0 } } } .fi .RE .PP The file bank/main.sg: .PP .RS 4 .nf use accounts void main() { var account = Account("Ada") var other = account with { note = "rent" } print(other.note ?? "") } .fi .RE .PP .RS 4 .nf $ cd bank $ sg main.sg error: `cents` is not public, so an `Account` cannot be copied with `with` outside the `accounts` module --> main.sg:5:17 5 | var other = account with { note = "rent" } | ^^^^^^^ .fi .RE .PP \fBwith\fR is a literal that writes every field, and \fBmain.sg\fR cannot write \fBcents\fR\&. Inside \fBaccounts\fR, the same \fBwith\fR is allowed. When every field is \fBpublic\fR, even a \fBreadonly\fR one may be given a new value this way, as a literal could. .SH "SEE ALSO" .BR sg-struct (7), .BR sg-this (7), .BR sg-readonly (7), .BR sg-public (7), .BR sg-read (7) .PP https://sg\-lang.org/reference/with.html