.\" Made by website/plain/build.mjs from website/words/this.md; change that file. .TH SG-THIS 7 2026-11-28 "SG" "SG reference" .SH NAME this \- the value a method was called on; read this looks, ref this changes, this alone takes it .SH SYNOPSIS .TP \fBT m(read this)\fR a method that only looks at the value .TP \fBvoid m(ref this)\fR a method that may change the value, or give it a new one with this = ... .TP \fBT m(this)\fR a method that takes the value: the caller's variable is used up, a Copyable one copied .TP \fBthis.field, this.m()\fR a field of the value, and another method called on it .TP \fBmatch this, this is Red\fR in a method of an enum, the variant it was called on .TP \fBreturn this, this with { ... }\fR in a method that takes this, the value given back, as it is or changed .TP \fBvoid format(read this, ref Formatter f), void dispose(ref this)\fR the methods the built-in interfaces ask for .TP \fBread shared T this\fR a method that borrows the owner, so that it can write share this: see share .SH DESCRIPTION \fBthis\fR is the value a method was called on. It is the method's first parameter, and the way it is written says what the method does with the value, as for any other parameter: \fBread this\fR borrows it to look, \fBref this\fR borrows it to change, and \fBthis\fR alone takes it. The call writes nothing for it: \fBcart.add("tea")\fR is the same whether \fBadd\fR takes \fBref this\fR or \fBread this\fR\&. .PP A field is always reached through \fBthis.\fR, and so is another method on the same value: \fBthis.count\fR, \fBthis.log()\fR\&. A bare \fBcount\fR is a local variable, so a field and a variable never look alike. \fBthis\fR is not a name a program can give: a function without a \fBthis\fR parameter has none, belongs to the type and is called on it, \fBCounter.create()\fR\&. .PP A method that takes \fBthis\fR owns the value: it may change it, move its fields out, and give it back with \fBreturn this\fR\&. The caller's variable is used up, unless the type is \fBCopyable\fR; then the method gets a copy. A \fBread this\fR or \fBref this\fR method only borrows the value, so it moves no field out of it: the caller still owns it whole. \fBtake()\fR and \fBreplace\fR give a field's value and leave another in its place. .PP A method is called through a \fBT*\fR as on the value itself; one that takes \fBthis\fR uses the pointer up. Methods of an enum take \fBthis\fR the same three ways, and \fBmatch this\fR or \fBthis is Red\fR says which variant the method was called on. .SH EXAMPLES .SS "The three ways" .RS 4 .nf struct Counter { string name int count int value(read this) { return this.count } void add(ref this, int by) { this.count += by this.log() } void log(read this) { print($"{this.name} = {this.value()}") } string finish(this) { return $"{this.name}: {this.count}".toString() } } void main() { var hits = Counter { name = "hits", count = 0 } hits.add(2) hits.add(3) var report = hits.finish() print(report) } .fi .RE .PP .RS 4 .nf hits = 2 hits = 5 hits: 5 .fi .RE .PP \fBadd\fR changes the counter and calls \fBlog\fR on it; a \fBref this\fR lends itself where a \fBread this\fR is asked for. \fBfinish\fR takes the counter, so \fBhits\fR cannot be used after that line. .SS "A Copyable value, and a method of an interface" .RS 4 .nf struct Money : Copyable, Formattable { long cents Money plus(this, Money other) { return Money { cents = this.cents + other.cents } } void format(read this, ref Formatter f) { f.write($"{this.cents / 100}.{this.cents % 100:d2} EUR") } } void main() { var price = Money { cents = 1250 } var total = price.plus(Money { cents = 199 }) print($"{price} + 1.99 = {total}") } .fi .RE .PP .RS 4 .nf 12.50 EUR + 1.99 = 14.49 EUR .fi .RE .PP \fBplus\fR takes \fBthis\fR, but \fBMoney\fR is \fBCopyable\fR, so it gets a copy and \fBprice\fR is still there. \fBformat\fR is the method \fBFormattable\fR asks for; \fBprint\fR and \fB$"{price}"\fR call it. .SS "An enum" .RS 4 .nf enum Light : Formattable { Red Yellow Green void advance(ref this) { this = match this { Red => Light.Green Yellow => Light.Red Green => Light.Yellow } } bool stops(read this) { return this is Red } } void main() { var light = Light.Red for _ in 0..4 { print($"{light} {light.stops()}") light.advance() } } .fi .RE .PP .RS 4 .nf Red true Green false Yellow false Red true .fi .RE .PP \fBmatch this\fR looks at the variant the method was called on. \fBthis = ...\fR in a \fBref this\fR method gives the caller's variable a new value. .SS "Giving this back" .RS 4 .nf struct Request : Formattable { string url List headers int timeout = 30 Request create(string url) { return Request { url = url, headers = List() } } Request header(this, string line) { this.headers.add(line) return this } Request waitFor(this, int seconds) { return this with { timeout = seconds } } } void main() { var request = Request("https://sg-lang.org") .header("Accept: text/html") .header("User-Agent: sg") .waitFor(5) print(request) } .fi .RE .PP .RS 4 .nf Request { url = "https://sg-lang.org", headers = ["Accept: text/html", "User-Agent: sg"], timeout = 5 } .fi .RE .PP Each method takes the request, changes it and gives it back, so the calls are chained with nothing copied: the same value goes from one to the next. .SS "A borrow of a field" .RS 4 .nf struct Contact { string name List phones read string bestPhone(read this) { if this.phones.length() == 0 { return read this.name } return this.phones[0] } } void main() { var ada = Contact { name = "Ada", phones = List { "555-0101", "555-0199" } } var bob = Contact { name = "Bob", phones = List() } var phone = ada.bestPhone() print(phone) print(bob.bestPhone()) ada.phones.clear() } .fi .RE .PP .RS 4 .nf 555-0101 Bob .fi .RE .PP \fBbestPhone\fR lends a text that lives in the contact, with no copy. While \fBphone\fR is used, \fBada\fR is lent: the \fBclear()\fR is allowed only after the last use of \fBphone\fR\&. .SS "Taking a field through ref this" .RS 4 .nf struct Outbox { string? draft List sent void send(ref this) { var text = this.draft.take() ?? return this.sent.add(text) } List flush(ref this) { return this.sent.replace(List()) } } void main() { var box = Outbox { draft = "hello", sent = List() } box.send() box.send() box.draft = "bye" box.send() print(box.flush()) print(box.sent.length()) print(box.draft ?? "no draft") } .fi .RE .PP .RS 4 .nf ["hello", "bye"] 0 no draft .fi .RE .PP \fBtake()\fR gives the text and leaves \fBnull\fR in \fBdraft\fR; \fBreplace\fR gives the list and leaves an empty one in \fBsent\fR\&. The second \fBsend\fR finds no draft and returns. The outbox is never left with a hole. .SS "Behind a pointer, with dispose" .RS 4 .nf struct Connection : Disposable { string host int sent void send(ref this, read string text) { this.sent += (int)text.length() } void dispose(ref this) { print($"close {this.host} after {this.sent} bytes") } } void main() { var link = new Connection { host = "db", sent = 0 } link.send("hello") link.send("bye") print("done") } .fi .RE .PP .RS 4 .nf done close db after 8 bytes .fi .RE .PP \fBlink\fR is a \fBConnection*\fR, and its methods are called as on the value. \fBdispose\fR is the method \fBDisposable\fR asks for: the program never calls it, and it runs once where the last owner ends, with \fBthis\fR the value that ends. .SH "WHAT THE COMPILER REFUSES" .SS "Writing through read this" .RS 4 .nf struct Counter { int count int bump(read this) { this.count += 1 return this.count } } void main() { var c = Counter { count = 0 } print(c.bump()) } .fi .RE .PP .RS 4 .nf error: this field is reached through a `read` borrow and cannot be written --> example.sg:5:9 5 | this.count += 1 | ^^^^^^^^^^ hint: make `bump` take `ref this` instead of `read this` .fi .RE .PP \fBint bump(ref this)\fR makes the program right. .SS "A field without this." .RS 4 .nf struct Counter { int count int value(read this) { return count } } void main() { var c = Counter { count = 0 } print(c.value()) } .fi .RE .PP .RS 4 .nf error: there is no variable named `count` --> example.sg:5:16 5 | return count | ^^^^^ hint: a field is reached through `this`: `this.count` .fi .RE .SS "this in a function that has none" .RS 4 .nf struct Counter { int count Counter zero() { return Counter { count = this.count } } } void main() { var c = Counter.zero() print(c.count) } .fi .RE .PP .RS 4 .nf error: there is no `this` here `this` exists in a method that takes it as its first parameter: `this`, `read this` or `ref this` --> example.sg:5:34 5 | return Counter { count = this.count } | ^^^^ .fi .RE .PP \fBzero\fR has no \fBthis\fR parameter: it belongs to the type and is called as \fBCounter.zero()\fR, on no value. .SS "this anywhere but first" .RS 4 .nf struct Counter { int count int plus(int by, read this) { return this.count + by } } void main() { var c = Counter { count = 0 } print(c.plus(1)) } .fi .RE .PP .RS 4 .nf error: `this` must be the first parameter --> example.sg:4:22 4 | int plus(int by, read this) { | ^^^^^^^^^ .fi .RE .SS "this as a name" .RS 4 .nf void main() { var this = 3 print(this) } .fi .RE .PP .RS 4 .nf error: `this` is the receiver of a method and cannot name a variable --> example.sg:2:9 2 | var this = 3 | ^^^^ .fi .RE .SS "A field moved out through ref this" .RS 4 .nf struct Outbox { string? draft string? send(ref this) { return this.draft } } void main() { var box = Outbox { draft = "hello" } print(box.send() ?? "nothing") } .fi .RE .PP .RS 4 .nf error: a `string?` field cannot be moved out through a borrow --> example.sg:5:16 5 | return this.draft | ^^^^^^^^^^ hint: take it with `this.draft.take()`, which leaves `null` there .fi .RE .PP The caller still owns the outbox, and its \fBdraft\fR must hold a value when \fBsend\fR ends. For a field that is not a \fBT?\fR, the hint is \fBreplace\fR, which leaves the value it is given. .SS "A method that takes this, called on a borrow" .RS 4 .nf struct Ticket { string code string spend(this) { return this.code } void redeem(ref this) { print(this.spend()) } } void main() { var t = Ticket { code = "A1" } t.redeem() } .fi .RE .PP .RS 4 .nf error: `spend` takes `this` by value and uses it up, but this value is only borrowed --> example.sg:9:15 9 | print(this.spend()) | ^^^^ hint: make `spend` take `read this` if it only looks, or make `redeem` take `this` by value .fi .RE .PP \fBredeem\fR borrows the ticket, so it cannot give it away. Either \fBredeem\fR takes \fBthis\fR too, or \fBspend\fR takes \fBread this\fR and gives \fBread string\fR back. .SH "SEE ALSO" .BR sg-read (7), .BR sg-ref (7), .BR sg-struct (7), .BR sg-enum (7), .BR sg-with (7), .BR sg-share (7), .BR sg-pointer (7) .PP https://sg\-lang.org/reference/this.html