.\" Made by website/plain/build.mjs from website/words/interpolation.md; change that file. .TH SG-INTERPOLATION 7 2026-11-28 "SG" "SG reference" .SH NAME $"{x}" \- text with values written into it, checked when the program is compiled; .toString() makes it a string .SH SYNOPSIS .TP \fB$"{name} is {age}"\fR values written into a text .TP \fB$"{age + 2}", $"{xs.length()}"\fR any expression in the braces .TP \fB$"{(c ? a : b)}"\fR ?: in parentheses, since a : starts the format .TP \fB{x,8}, {x,\-8}\fR a width: spaces in front, or after .TP \fB{n:x}, {n:X4}, {n:b8}, {n:D3}\fR an integer in hex, in binary, with at least so many digits .TP \fB{d:F2}, {d:e3}, {d:g}\fR a fractional number: digits after the point, with an exponent, the shorter of the two .TP \fB{d,10:F2}\fR a width and a letter .TP \fB{{, }}\fR a brace .TP \fB$"""..."""\fR raw text with values: no escapes .TP \fB$"...".toString()\fR the text as a new string .SH DESCRIPTION \fB$"..."\fR writes values into a text. Each \fB{ }\fR holds an expression, and its value is written where the braces are. The values are computed from left to right, all of them before the text is written. A value can be anything \fBprint\fR takes: a number, a \fBbool\fR, a \fBchar\fR, a text, a list, a struct or an enum that is \fBFormattable\fR\&. A \fBT?\fR is not one: it is given a fallback with \fB??\fR first. .PP It is part of the language and checked when the program is compiled: the format, and that each letter fits its value. Nothing of it is read while the program runs. .PP A \fB$"..."\fR is not a \fBstring\fR\&. \fBprint\fR, \fBpanic\fR, a text's \fBwrite\fR, a \fBFormatter\fR\&'s \fBf.write\fR and the writers of files take it as it is and write it without making one. Where a \fBstring\fR is needed, \fB\&.toString()\fR makes it, and that is where the allocation shows. Texts are joined this way too: \fBa + b\fR is refused, \fB$"{a}{b}"\fR is the join. .PP A \fB$"..."\fR borrows its values until it is written. Kept in a variable, \fBvar line = $"hi {name}"\fR, it is a borrow like \fBread name\fR: until the last use of \fBline\fR, \fBname\fR is not moved or changed, and a value with no name, as \fB{n + 1}\fR or \fB{make()}\fR, cannot be kept, since it is gone when the line ends. A number or a text written in the braces is kept as it is. \fB\&.toString()\fR writes the text out at once, and keeps no borrow. \fBsg build\fR does not compile a \fB$"..."\fR kept in a variable yet. .PP A width follows a comma: \fB{x,8}\fR takes at least eight characters, with spaces in front; \fB{x,\-8}\fR puts the spaces after. It counts characters, not bytes, and a longer value is not cut. The width is a number written in the text, at most 999. .PP A letter follows a colon. \fBx\fR and \fBX\fR write an integer in hex, \fBb\fR in binary, \fBD\fR in decimal; the number after the letter is the digits there are at least, zeros in front and the sign before them. A negative number in hex or binary is written as the bits of its type, with no \fB0x\fR\&. \fBF\fR writes a fractional number with so many digits after the point, and needs the number. \fBe\fR writes it with an exponent and so many digits after the point, \fBg\fR with so many significant digits in the shorter of the two ways, zeros at the end left out; without a number both take 6, and \fBE\fR and \fBG\fR write a capital \fBE\fR\&. The number is at most 999. A letter that does not fit the value is an error. There is no \fBN\fR, whose separators change from one country to another: a program writes the same text everywhere. .PP \fBF\fR rounds the exact value to the nearest, and a tie goes to the even digit: \fB{2.5:F0}\fR is \fB2\fR\&. That is not what \fBround()\fR does, which takes a half away from zero; \fBroundToEven()\fR gives the digits of \fBF\fR\&. \fBNaN\fR and the infinities are written as \fBprint\fR writes them, and a small negative number keeps its minus. .PP Inside the braces a \fB:\fR outside the value's own brackets starts the letter, and a \fB,\fR starts the width when a number follows it; otherwise it stays part of a type, as in \fBHashMap\fR\&. Text in quotes may be written in the braces. Outside them, the escapes are those of \fB"..."\fR\&. \fB$"""..."""\fR is raw text, with values put in the same way and \fB{{\fR for a brace. .SH EXAMPLES .SS "Values in a text" .RS 4 .nf struct Point : Formattable { int x int y } void main() { var name = "Pasha" var age = 4 var toys = List { "ball", "mouse" } print($"{name} is {age}") print($"in two years: {age + 2}, toys: {toys.length()}") print($"{toys} {toys.contains("ball")} {'P'} {1.5}") print($"at {Point { x = 3, y = -4 }}") print($"{age} {(age == 1 ? "year" : "years")}") } .fi .RE .PP .RS 4 .nf Pasha is 4 in two years: 6, toys: 2 ["ball", "mouse"] true P 1.5 at Point { x = 3, y = -4 } 4 years .fi .RE .PP A list writes its texts in quotes, and a struct its fields. \fBtoys.contains("ball")\fR has quotes of its own inside the braces. .SS "Letters" .RS 4 .nf void main() { int n = 255 int day = 7 double price = 3.14159 print($"{n:x} {n:X4} {n:b} {n:b12}") print($"{day:D3} {-day:D3} {price:F2} {price:F0}") print($"{price:e2} {price:g3} {1234567.0:g} {0.0001:G}") print($"{(sbyte)-1:x} {-1:x} {(long)-1:X}") } .fi .RE .PP .RS 4 .nf ff 00FF 11111111 000011111111 007 -007 3.14 3 3.14e+00 3.14 1.23457e+06 0.0001 ff ffffffff FFFFFFFFFFFFFFFF .fi .RE .PP \fB\-1\fR is written as the bits of its type: two digits for an \fBsbyte\fR, eight for an \fBint\fR, sixteen for a \fBlong\fR\&. .SS "Columns" .RS 4 .nf struct Item { string name int count double price } void main() { var items = List { Item { name = "tea", count = 3, price = 4.5 }, Item { name = "Zürich map", count = 12, price = 12.25 }, Item { name = "pen", count = 140, price = 0.8 }, } print($"[{"name",-12}] [{"count",5}] [{"price",8}]") for item in read items { print($"[{item.name,-12}] [{item.count,5}] [{item.price,8:F2}]") } print($"[{"longer than the width",4}]") } .fi .RE .PP .RS 4 .nf [name ] [count] [ price] [tea ] [ 3] [ 4.50] [Zürich map ] [ 12] [ 12.25] [pen ] [ 140] [ 0.80] [longer than the width] .fi .RE .PP \fBZürich map\fR has eleven bytes and ten characters, and gets two spaces. The last value is longer than its width and is written whole. .SS "How F rounds" .RS 4 .nf void main() { var prices = List { 0.5, 1.5, 2.5, -2.5, 0.125, 2.675 } for p in prices { print($"{p,6} {p,4:F0} {p.round(),4} {p.roundToEven(),4} {p,6:F2}") } print($"{-0.001:F2} {1.0 / 0.0:F2} {0.0 / 0.0:F2}") } .fi .RE .PP .RS 4 .nf 0.5 0 1 0 0.50 1.5 2 2 2 1.50 2.5 2 3 2 2.50 -2.5 -2 -3 -2 -2.50 0.125 0 0 0 0.12 2.675 3 3 3 2.67 -0.00 Infinity NaN .fi .RE .PP A half goes to the even digit, as \fBroundToEven()\fR does, not away from zero, as \fBround()\fR does. \fB2.675\fR is written \fB2.67\fR: the \fBdouble\fR nearest to it is a little below it, and \fBF\fR rounds that exact value. .SS "Braces, quotes and raw text" .RS 4 .nf void main() { var name = "Pasha" string? nickname = null print($"{{name}} is written {name}") print($"a set: {{ {name} }}") print($"{"quoted"} and {nickname ?? "no nickname"}") print($"""{name} said "meow" in C:\ecats\e{name}""") print($""" name: "{name}" size: {name.length(),3} """) } .fi .RE .PP .RS 4 .nf {name} is written Pasha a set: { Pasha } quoted and no nickname Pasha said "meow" in C:\ecats\ePasha name: "Pasha" size: 5 .fi .RE .PP In raw text a backslash is a backslash and a quote is a quote. On lines, the indentation of the closing quotes is taken off every line, as in \fB"""..."""\fR\&. .SS "A string, a builder and a Formatter" .RS 4 .nf struct Money : Formattable { long cents void format(read this, ref Formatter f) { f.write($"{this.cents / 100}.{this.cents % 100:D2} EUR") } } int noisy(int n) { print($" computing {n}") return n * 10 } void main() { var price = Money { cents = 1905 } print($"[{price}] [{price,14}]") string label = $"total: {price}".toString() print($"{label} ({label.length()} bytes)") var line = string.withCapacity(32) for i in 1..4 { line.write($"{i}:{i * i};") } print(line) print($"first {noisy(1)}, then {noisy(2)}") } .fi .RE .PP .RS 4 .nf [19.05 EUR] [ 19.05 EUR] total: 19.05 EUR (16 bytes) 1:1;2:4;3:9; computing 1 computing 2 first 10, then 20 .fi .RE .PP \fBMoney\fR writes itself with a \fB$"..."\fR, and a width works on it as on a number. \fB\&.toString()\fR makes the one \fBstring\fR of the program; \fBwrite\fR adds each \fB$"..."\fR to \fBline\fR without making another. Both calls of \fBnoisy\fR run before the line is written. .SH "WHAT THE COMPILER REFUSES" .SS "A string without .toString()" .RS 4 .nf string label(int count) { return $"{count} items" } void main() { print(label(3)) } .fi .RE .PP .RS 4 .nf error: expected `string`; `$"..."` does not become a `string` on its own --> example.sg:2:12 2 | return $"{count} items" | ^^^^^^^^^^^^^^^^ hint: add `.toString()` at the end; that is where the allocation shows .fi .RE .PP A \fBread string\fR parameter is given \fBread $"...".toString()\fR\&. A \fB$"..."\fR has no other method, and is compared with \fB==\fR once it is a \fBstring\fR too. .SS "A value moved while a kept $""..."" looks at it" .RS 4 .nf void main() { var name = "Ada".clone() var line = $"hi {name}" var taken = name print(taken) print(line) } .fi .RE .PP .RS 4 .nf error: `name` cannot be moved here: `line` borrows from it and is used again later --> example.sg:4:17 4 | var taken = name | ^^^^ `line` borrowed it here: --> example.sg:3:22 3 | var line = $"hi {name}" | ^^^^ hint: finish with `line` before this line: a borrow ends at its last use .fi .RE .PP \fBprint(line)\fR before the move, or \fB$"hi {name}".toString()\fR, which keeps no borrow, is what works. .SS "A text without $" .RS 4 .nf void main() { var count = 3 print("{count} items") } .fi .RE .PP .RS 4 .nf error: `count` is never used `"{count} items"` is written as it is; a value goes into a text with `$`: `$"{count} items"` --> example.sg:2:9 2 | var count = 3 | ^^^^^ .fi .RE .SS "?: without parentheses" .RS 4 .nf void main() { var count = 1 print($"{count} {count == 1 ? "file" : "files"}") } .fi .RE .PP .RS 4 .nf error: `c ? a : b` inside `{ }` is written in parentheses a `:` there starts the format, as in `{x:X}` --> example.sg:3:33 3 | print($"{count} {count == 1 ? "file" : "files"}") | ^^^^^^^^^^ hint: write `{(c ? a : b)}` .fi .RE .SS "A letter that does not fit" .RS 4 .nf void main() { int total = 19 print($"{total:F2}") } .fi .RE .PP .RS 4 .nf error: `F` writes a fractional number with so many digits after the point, and `total` is an `int` --> example.sg:3:19 3 | print($"{total:F2}") | ^^^ hint: make it a `double` first: `{(double)total:F2}` .fi .RE .PP \fBx\fR, \fBX\fR, \fBb\fR and \fBD\fR are refused the same way on a \fBdouble\fR, a \fBchar\fR or a text. .SS "A value that may be null" .RS 4 .nf void main() { var scores = List { 7, 9 } print($"third: {scores.get(2)}") } .fi .RE .PP .RS 4 .nf error: this is `int?` and may be `null`; a `T?` alone is not written as text inside a list or a struct a missing value is written `null` --> example.sg:3:21 3 | print($"third: {scores.get(2)}") | ^^^^^^^^^^^^^ hint: give a fallback, `scores.get(2) ?? ...`, or keep it in a variable and test that: `var value = scores.get(2)`, then `if value is not null { ... }` .fi .RE .PP \fB{scores.get(2) ?? 0}\fR writes \fB0\fR\&. .SS "A width in a variable" .RS 4 .nf void main() { var width = 8 print($"[{"tea",width}]") } .fi .RE .PP .RS 4 .nf error: expected `}`, found `,` a width after `,` is a number written in the text: `{n,8}` --> example.sg:3:20 3 | print($"[{"tea",width}]") | ^ .fi .RE .SS "A brace alone" .RS 4 .nf void main() { var name = "Pasha" print($"{name} }") } .fi .RE .PP .RS 4 .nf error: unmatched `}` --> example.sg:3:20 3 | print($"{name} }") | ^ hint: write `}}` for a literal brace .fi .RE .SS "A letter that is not there" .RS 4 .nf void main() { double total = 1234567.891 print($"{total:N2}") } .fi .RE .PP .RS 4 .nf error: `N` is not a format here `N` would put separators between groups of digits, which change from one country to another --> example.sg:3:20 3 | print($"{total:N2}") | ^^ hint: an integer is written as it is: `{total}`; a fractional number with so many digits after the point: `{total:F2}` .fi .RE .SH "SEE ALSO" .BR sg-string (7), .BR sg-char (7), .BR sg-double (7), .BR sg-bool (7), .BR sg-conditional (7), .BR sg-null-coalescing (7), .BR sg-interface (7) .PP https://sg\-lang.org/reference/interpolation.html