.\" Made by website/plain/build.mjs from website/words/const.md; change that file. .TH SG-CONST 7 2026-11-28 "SG" "SG reference" .SH NAME const \- a value fixed when the program is compiled .SH SYNOPSIS .TP \fBconst T Name = value\fR a const at the top of a file, seen by the whole module .TP \fBconst T Name = value in a block\fR a const seen to the end of the block .TP \fBpublic const T Name = value\fR a const other modules can read .TP \fBconst int[] Name = { 1, 2, 3 }\fR a table; the count of its items is found from them (int[3]) .TP \fBconst byte[] Name = embed("file")\fR the bytes of a file beside the source, read when the program is compiled .TP \fBT[Name]\fR an array whose count is an integer const .TP \fBName => ...\fR in a match, an arm for the value of the const .TP \fBconst assert(condition)\fR a condition checked when the program is compiled (see assert) .SH DESCRIPTION A \fBconst\fR is a value the compiler finds before the program runs. Its type is always written. It holds a number, a \fBbool\fR, a \fBchar\fR or a text, or an array, a \fBCopyable\fR struct or a \fBCopyable\fR enum of them. A \fBList\fR, an owned \fBstring\fR, a pointer and a \fBshared\fR value cannot be one. .PP Its value is made of numbers, texts, other consts, operators and casts, \fBMinValue\fR, \fBMaxValue\fR, \fBPi\fR and their kin, arrays, structs and variants, \fB\&.length()\fR of a const array or text, and an item or a field of one. Consts may name each other in any order, but not in a circle. A const calls no function, so the compiler runs none of the program's code; a value a function finds is a [\fBstatic\fR](static.md). An overflow, a division by zero or an index outside the array is a compile error, not a panic. .PP Every use reads the one value. \fBvar copy = Table\fR copies it, and \fBTable[i]\fR reads it where it lies. Nothing can write into a const. A local cannot take the name of a const it sees, so the name means the const everywhere, in a \fBmatch\fR too. By custom the name is in PascalCase. .PP In compiled code a number is written where it is used, and a table is one \fBstatic const\fR of C, in the data the program only reads. .SH EXAMPLES .SS "Numbers, text, bool and char" .RS 4 .nf const int Width = 80 const int Margin = 4 const int Text = Width - 2 * Margin const double Ratio = Text / 2.0 const string Title = "report" const bool Verbose = false const char Bullet = '*' void main() { print($"{Title}: {Text} columns, ratio {Ratio}") if !Verbose { print($"{Bullet} quiet") } } .fi .RE .PP .RS 4 .nf report: 72 columns, ratio 36 * quiet .fi .RE .PP \fBText\fR is computed from two other consts. All of it is found when the program is compiled; nothing is computed when it runs. .SS "In any order, with casts" .RS 4 .nf const int Total = Half * 2 const int Half = 21 const byte Low = (byte)(Total & 0xff) const long Wide = (long)int.MaxValue + 1 const double Circle = 2.0 * double.Pi void main() { print($"{Total} {Low} {Wide}") print($"{Circle:f4}") } .fi .RE .PP .RS 4 .nf 42 42 2147483648 6.2832 .fi .RE .PP \fBTotal\fR names \fBHalf\fR, which comes after it. \fB(long)\fR widens before the addition, so \fBWide\fR does not overflow. .SS "In a block, as the count of an array" .RS 4 .nf void main() { const int Rows = 3 const int Cols = Rows + 1 const int Cells = Rows * Cols int[Cells] grid = int[Cells](1) var row = int[Cols](0) print($"{grid.length()} {row}") } .fi .RE .PP .RS 4 .nf 12 [0, 0, 0, 0] .fi .RE .PP A const in a block is seen to the end of the block. An integer const gives the count of an array; the count is a number or the name of a const, so \fBCells\fR is written for \fBRows * Cols\fR\&. .SS "A table" .RS 4 .nf const int[] Primes = { 2, 3, 5, 7, 11 } const nint Count = Primes.length() const int Largest = Primes[Count - 1] void main() { var sum = 0 for p in Primes { sum += p } var copy = Primes copy[0] = 1 print($"{Count} primes up to {Largest}, sum {sum}") print($"{copy} {Primes}") } .fi .RE .PP .RS 4 .nf 5 primes up to 11, sum 28 [1, 3, 5, 7, 11] [2, 3, 5, 7, 11] .fi .RE .PP \fBint[]\fR after \fBconst\fR counts the items, so \fBPrimes\fR is an \fBint[5]\fR\&. \fBlength()\fR gives an \fBnint\fR\&. \fBcopy\fR is a copy of its own, and changing it leaves \fBPrimes\fR as it was. .SS "A struct and an enum" .RS 4 .nf struct Size : Copyable { int width int height } enum Shade : Copyable, Formattable { Light Dark } const Size Page = Size { width = 210, height = 297 } const int Area = Page.width * Page.height const Shade Theme = Shade.Dark void main() { print($"{Page.width} x {Page.height} = {Area}") print(Theme) } .fi .RE .PP .RS 4 .nf 210 x 297 = 62370 Dark .fi .RE .PP The struct and the enum must be \fBCopyable\fR: a const is a value that can be copied with no cost. A field of a const struct can be used in another const. .SS "In a match" .RS 4 .nf const int Fine = 200 const int NotFound = 404 const string Quit = "quit" string status(int code) { return match code { Fine => "fine" NotFound => "not found" _ => $"code {code}".toString() } } string answer(string line) { return match line { Quit => "bye" _ => "go on" } } void main() { print(status(200)) print(status(404)) print(status(500)) print(answer("quit")) print(answer("next")) } .fi .RE .PP .RS 4 .nf fine not found code 500 bye go on .fi .RE .PP A const of a number, a \fBchar\fR, a text or a \fBbool\fR stands for its value in a pattern. The arm \fBFine\fR is taken when \fBcode\fR is 200; it does not name a new variable. .SS "Checked when the program is compiled" .RS 4 .nf const int[] Masks = { 0x01, 0x02, 0x04, 0x08 } const int Bits = 4 const assert(Masks.length() == Bits, "one mask for each bit") void main() { var all = 0 for m in Masks { all |= m } print(all) } .fi .RE .PP .RS 4 .nf 15 .fi .RE .PP A \fBconst assert\fR is made of consts, as a const is. Had \fBMasks\fR three items, the program would not compile: \fBconst assert failed: one mask for each bit\fR, with both sides shown. .SS "From another module" The file screen/limits/sizes.sg: .PP .RS 4 .nf public const int Columns = 80 public const string Ruler = "----" const int Hidden = 3 .fi .RE .PP The file screen/main.sg: .PP .RS 4 .nf use limits void main() { print($"{Columns} columns") print(limits.Ruler) } .fi .RE .PP .RS 4 .nf $ cd screen $ sg main.sg 80 columns ---- .fi .RE .PP \fBpublic\fR lets other modules read a const, by its bare name after \fBuse\fR or by its full name. \fBHidden\fR stays inside \fBlimits\fR: \fBmain.sg\fR naming it gets \fB`Hidden` exists in the `limits` module but is not public\fR\&. .SS "The bytes of a file" The file motd/motd.txt: .PP .RS 4 .nf Welcome to the box. .fi .RE .PP The file motd/main.sg: .PP .RS 4 .nf const byte[] Motd = embed("motd.txt") void main() { var text = string.fromUtf8(Motd) ?? "not UTF-8" print(text.trimEnd()) print($"{Motd.length()} bytes") } .fi .RE .PP .RS 4 .nf $ cd motd $ sg build main.sg -o motd $ rm motd.txt $ ./motd Welcome to the box. 20 bytes $ sg main.sg error: there is no file `motd.txt` beside this file it is looked for at motd.txt --> main.sg:1:21 1 | const byte[] Motd = embed("motd.txt") | ^^^^^^^^^^^^^^^^^ .fi .RE .PP \fBembed\fR reads the file when the program is compiled, so the built program keeps the bytes after the file is gone. The path is written out, with \fB/\fR, from the folder of the source file; the file is in that folder or below it, and holds 1 byte to 64 MB. .SH "WHAT THE COMPILER REFUSES" .SS "A call" .RS 4 .nf int twice(int n) { return n * 2 } const int Size = twice(32) void main() { print(Size) } .fi .RE .PP .RS 4 .nf error: a const calls no function a const is made of numbers, texts, other consts, operators and casts, and arrays, structs and enums of them --> example.sg:5:18 5 | const int Size = twice(32) | ^^^^^ hint: a value a function finds is a `static`, found when the program first uses it: `static T Name = f()` .fi .RE .SS "A variable" .RS 4 .nf void main() { var n = 8 const int Size = n * 2 print(Size) } .fi .RE .PP .RS 4 .nf error: there is no variable named `n` a const sees other consts, not variables: its value is found when the program is compiled --> example.sg:3:22 3 | const int Size = n * 2 | ^ .fi .RE .PP A static cannot be read either: \fB`Base` is a static, and a const cannot read it\fR\&. .SS "Writing into it" .RS 4 .nf const int[] Primes = { 2, 3, 5 } void main() { Primes[0] = 1 } .fi .RE .PP .RS 4 .nf error: `Primes` is a const: what it holds is fixed when the program is compiled --> example.sg:4:5 4 | Primes[0] = 1 | ^^^^^^^^^ hint: change a copy of it: `var copy = Primes` .fi .RE .PP \fBPrimes = ...\fR and \fBref Primes\fR are refused the same way, and so is a method that changes it: .PP .RS 4 .nf const int[] Primes = { 5, 3, 2 } void main() { Primes.sort() } .fi .RE .PP .RS 4 .nf error: `sort` changes the array, but it is reached through the const `Primes` --> example.sg:4:5 4 | Primes.sort() | ^^^^^^ hint: change a copy of it: `var copy = Primes` .fi .RE .PP A const text is refused the same way: \fBName.clear()\fR and \fBName.write(...)\fR change the text, so they are not called on a const (\fBvar copy = Name\fR, then \fBcopy.clear()\fR, is). A method that only reads, \fBName.length()\fR, is. .SS "A const of a block that nothing uses" .RS 4 .nf void main() { const int Unused = 1 print("x") } .fi .RE .PP .RS 4 .nf error: `Unused` is never used --> example.sg:2:15 2 | const int Unused = 1 | ^^^^^^ hint: take the line away: a const of a block is seen only by the lines after it in the block .fi .RE .PP As for a variable nothing reads. A line that names it anywhere after it in the block uses it: another const, the count of an array, a \fB$"..."\fR\&. .SS "A calculation as the count of an array" .RS 4 .nf const int Rows = 2 const int Cols = 3 void main() { int[Rows * Cols] grid = int[Rows * Cols](1) print(grid.length()) } .fi .RE .PP .RS 4 .nf error: the count of an array is a number or the name of a const, not a calculation --> example.sg:5:9 5 | int[Rows * Cols] grid = int[Rows * Cols](1) | ^^^^^^^^^^^ hint: give it a name first: `const int Count = Rows * Cols`, then `[Count]` .fi .RE .SS "An overflow" .RS 4 .nf const int Big = int.MaxValue + 1 void main() { print(Big) } .fi .RE .PP .RS 4 .nf error: the value of `Big` is found when the program is compiled, and this stops it: overflow: 2147483647 + 1 does not fit in int --> example.sg:1:17 1 | const int Big = int.MaxValue + 1 | ^^^^^^^^^^^^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around .fi .RE .PP What would panic when the program runs stops the compiler here: a division by zero, and an index outside a const array, are errors too. .SS "A const with no type" .RS 4 .nf const Size = 64 void main() { print(Size) } .fi .RE .PP .RS 4 .nf error: a const has a type and a name: `const int Size = 64` --> example.sg:1:12 1 | const Size = 64 | ^ .fi .RE .SS "A value of another type" .RS 4 .nf const int Size = 1.5 void main() { print(Size) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `double` --> example.sg:1:18 1 | const int Size = 1.5 | ^^^ hint: convert with `(int)1.5`; it drops the fraction, and a value that does not fit panics, or becomes the nearest `int` in a release build .fi .RE .SS "A List" .RS 4 .nf const List Sizes = List { 1, 2 } void main() { print(Sizes) } .fi .RE .PP .RS 4 .nf error: a const cannot hold a `List`: a `List` is not known when the program is compiled a const holds numbers, `bool`, `char`, a text, and arrays, structs and enums of them --> example.sg:1:7 1 | const List Sizes = List { 1, 2 } | ^^^^^^^^^ .fi .RE .PP A list owns a block of the heap. A table is an array, and a list built once when the program runs is a [\fBstatic\fR](static.md). .SS "A const that needs itself" .RS 4 .nf const int A = B + 1 const int B = A + 1 void main() { print(A) } .fi .RE .PP .RS 4 .nf error: the value of `A` needs itself: `A` -> `B` -> `A` --> example.sg:2:15 2 | const int B = A + 1 | ^ hint: a const is given a value that does not lead back to it .fi .RE .SS "A local with the name of a const" .RS 4 .nf const int Limit = 10 void main() { var Limit = 3 print(Limit) } .fi .RE .PP .RS 4 .nf error: `Limit` is the name of a const, and a local cannot take it in a pattern the name stands for the const, so it means one thing in a function --> example.sg:4:9 4 | var Limit = 3 | ^^^^^ .fi .RE .PP The same holds for a const in a block with the name of one around it. .SH "SEE ALSO" .BR sg-static (7), .BR sg-var (7), .BR sg-assert (7), .BR sg-match (7), .BR sg-public (7) .PP https://sg\-lang.org/reference/const.html