.\" Made by website/plain/build.mjs from website/words/static.md; change that file. .TH SG-STATIC 7 2026-11-28 "SG" "SG reference" .SH NAME static \- a value made once, the first time the program uses it .SH SYNOPSIS .TP \fBstatic T Name = value\fR one value for the whole program, only read .TP \fBstatic atomic T Name = atomic value\fR one that tasks change with its atomic methods .TP \fBstatic locked T Name = value\fR one that tasks change inside a lock block .TP \fBpublic static T Name = value\fR a static other modules can reach .TP \fBstatic T Name = value in a function\fR one for the whole program, whose name is seen only in that block .SH DESCRIPTION A \fBstatic\fR is one value for the program. It lives until the program ends, and it is never assigned. Its type is always written. Every task can reach it, so it comes in three kinds: read only, \fBatomic\fR, and \fBlocked\fR\&. .PP When the value is made as a [\fBconst\fR](const.md) is, of numbers, texts, other consts and operators, it lies in the program's data from the start, at no cost. Any other value, one a function finds, is found the first time the static is used, once, whichever task comes first; a task that reads it meanwhile waits for it. A later read is one load and one branch. Nothing is found before \fBmain\fR, so a function that gives a static its value never runs when the program does not read the static. .PP A static of a number, a \fBbool\fR, a \fBchar\fR or a function is read as a value; any other is lent as \fBread\fR, so it can be looked at but not changed. A \fBstatic atomic\fR value changes through its methods. A \fBstatic locked\fR value is reached only in a \fBlock\fR block, where one task at a time holds it; it is the one place \fBlocked\fR is written without [\fBshared\fR](shared.md). .PP What finds a value cannot \fBlock\fR, \fBwait\fR or \fBawait\fR, so finding cannot deadlock, and it cannot need the static itself. What a static holds follows the rule of tasks: no borrow, no \fBTask\fR, no \fBshared checked\fR value outside a lock. It is never dropped, so it cannot hold a file or another value that must be closed. There is no global variable that any task assigns, and none for each thread. .SH EXAMPLES .SS "Found the first time it is used" .RS 4 .nf int[10] squares() { print("making the squares") var table = int[10] for n in 0..10 { table[n] = n * n } return table } static int[10] Squares = squares() void main() { print("start") print(Squares[3]) print(Squares[9]) } .fi .RE .PP .RS 4 .nf start making the squares 9 81 .fi .RE .PP \fBsquares\fR runs when \fBSquares[3]\fR is first read, after \fBstart\fR, and only once. A const could not be given this value: a const calls no function. .SS "Known from the start" .RS 4 .nf static int[4] Ports = int[4] { 80, 443, 8080, 8443 } static string Host = "localhost" void main() { for port in Ports { print($"{Host}:{port}") } } .fi .RE .PP .RS 4 .nf localhost:80 localhost:443 localhost:8080 localhost:8443 .fi .RE .PP These values are made as a const is, so they lie in the program from its start, and a read is not checked. .SS "Read by several tasks" .RS 4 .nf List load() { print("loading the words") return List { "static", "atomic", "locked" } } static List Words = load() int count(int extra) { return (int)Words.length() + extra } void main() { print("start") var a = Task.run(count, 10) var b = Task.run(count, 20) print(a.wait() + b.wait()) print(Words[0]) } .fi .RE .PP .RS 4 .nf start loading the words 36 static .fi .RE .PP Both tasks read \fBWords\fR, and \fBload\fR runs once: the task that comes first finds the list, and the other waits for it. A list cannot be a const, but it can be a static. Every use lends it as \fBread\fR, so a task may look at it and no task may change it. .SS "A pattern compiled once" .RS 4 .nf static Regex Port = Regex.of("""port (\ed+)""") void main() { var lines = List { "port 8080", "no port", "port 22" } for line in lines { var found = Port.find(read line) if found is not null { print(found.of(read line)) } } } .fi .RE .PP .RS 4 .nf port 8080 port 22 .fi .RE .PP The pattern is compiled the first time \fBPort\fR is read, not on each turn of the loop. .SS "A counter all tasks change" .RS 4 .nf static atomic long Requests = atomic 0 void serve(int times) { for _ in 0..times { _ = Requests.fetchAdd(1) } } void main() { var tasks = List>() for _ in 0..4 { tasks.add(Task.run(serve, 250)) } Task.whenAll(tasks).wait() print(Requests.load()) } .fi .RE .PP .RS 4 .nf 1000 .fi .RE .PP \fBstatic atomic\fR needs no owner and no \fBshare\fR: every task reaches the same counter, and no addition is lost. .SS "A static in a function" .RS 4 .nf int nextId() { static atomic int Last = atomic 0 return Last.fetchAdd(1) + 1 } void main() { print(nextId()) print(nextId()) print(nextId()) } .fi .RE .PP .RS 4 .nf 1 2 3 .fi .RE .PP There is one \fBLast\fR for the program, not one for each call. Its name is seen only inside \fBnextId\fR\&. .SS "Settings that change, under a lock" .RS 4 .nf struct Settings { string host int port } Settings load() { print("reading the settings") return Settings { host = "localhost", port = 80 } } static locked Settings Current = load() void usePort(int port) { lock Current as s { s.port = port } } void main() { print("start") var task = Task.run(usePort, 8080) task.wait() lock Current as s { print($"{s.host}:{s.port}") } } .fi .RE .PP .RS 4 .nf start reading the settings localhost:8080 .fi .RE .PP Inside \fBlock Current as s\fR, \fBs\fR is a \fBref\fR to the settings, and no other task is in a \fBlock\fR block of \fBCurrent\fR at the same time. Outside a \fBlock\fR block, \fBCurrent\fR cannot be reached at all. .SS "From another module" The file server/stats/counts.sg: .PP .RS 4 .nf public static atomic long Requests = atomic 0 public void count() { _ = Requests.fetchAdd(1) } .fi .RE .PP The file server/main.sg: .PP .RS 4 .nf use stats void main() { count() count() print(Requests.load()) } .fi .RE .PP .RS 4 .nf $ cd server $ sg main.sg 2 .fi .RE .PP \fBpublic\fR makes the static seen in every module that names \fBstats\fR; it is still one value for the program. .SS "Each test starts again" .RS 4 .nf static atomic int Hits = atomic 0 void hit() { _ = Hits.fetchAdd(1) } test "two hits" { hit() hit() assert(Hits.load() == 2) } test "each test starts with the statics as the program starts" { assert(Hits.load() == 0) hit() assert(Hits.load() == 1) } void main() { hit() print(Hits.load()) } .fi .RE .PP .RS 4 .nf ok two hits ok each test starts with the statics as the program starts 2 test(s) passed .fi .RE .PP What one test changed in a static is gone in the next, and a value found in one test is found again. \fBsg test \-\-compiled\fR prints the same, each test running in a process of its own. .SH "WHAT THE COMPILER REFUSES" .SS "Assigning a static" .RS 4 .nf static int Total = 3 void main() { Total += 1 } .fi .RE .PP .RS 4 .nf error: `Total` is a static, and a static is never assigned: it is read by every task a static that changes is `static atomic`, changed by its atomic methods, or `static locked`, changed inside a `lock` block --> example.sg:4:5 4 | Total += 1 | ^^^^^ .fi .RE .SS "A static with no type" .RS 4 .nf static var Limit = 3 void main() { print(Limit) } .fi .RE .PP .RS 4 .nf error: a static has its type written --> example.sg:1:8 1 | static var Limit = 3 | ^^^ .fi .RE .SS "A value that waits" .RS 4 .nf int work() { return 1 } int find() { var task = Task.run(work) return task.wait() } static int Value = find() void main() { print(Value) } .fi .RE .PP .RS 4 .nf error: the value of the static `Value` is found where it is first used, and `find` waits .fi .RE .PP Other tasks may be waiting for \fBValue\fR while it is found, so what finds it never waits: no \fBwait\fR, no \fBlock\fR, no \fBawait\fR, in any function it calls. .SS "A static that needs itself" .RS 4 .nf static int First = first() static int Second = second() int first() { return Second + 1 } int second() { return First + 1 } void main() { print(First) } .fi .RE .PP .RS 4 .nf error: the value of `First` needs itself: `First` -> `Second` -> `First` .fi .RE .PP The compiler follows the calls and the statics they read. A circle through a function value is found only when the program runs: .PP .RS 4 .nf static int Total = through(readTotal) int readTotal() { return Total + 1 } int through(Func give) { return give() } void main() { print("start") print(Total) } .fi .RE .PP .RS 4 .nf start panic: the value of `Total` needs itself: it is read while it is being found .fi .RE .SS "A task in a static" .RS 4 .nf int work() { return 1 } static Task Work = Task.run(work) void main() { } .fi .RE .PP .RS 4 .nf error: a static cannot hold a `Task`: a task is waited for by the task that started it, and by no other every task reaches a static, so what it holds owns all it holds: no borrow, no `shared checked` value, no `Task` .fi .RE .SS "A file in a static" .RS 4 .nf static StreamWriter Log = File.createText("log.txt") ?? panic("no log") void main() { } .fi .RE .PP .RS 4 .nf error: a static cannot hold a `StreamWriter`: a static is never dropped, so it would never be closed --> example.sg:1:8 1 | static StreamWriter Log = File.createText("log.txt") ?? panic("no log") | ^^^^^^^^^^^^ hint: open it where it is used and lend it to the functions that need it, as a parameter .fi .RE .SS "A type that is not one" .RS 4 .nf static File Log = File.create("log.txt") void main() { } .fi .RE .PP .RS 4 .nf error: there is no type named `File` `File` is not a type but a set of functions: a file open to read is a `StreamReader`, one open to write a `StreamWriter` --> example.sg:1:8 1 | static File Log = File.create("log.txt") | ^^^^ .fi .RE .SS "A static of a block that reads a variable" .RS 4 .nf void main() { var n = 3 static int Twice = n * 2 print(Twice) } .fi .RE .PP .RS 4 .nf error: there is no variable named `n` a static sees consts and other statics, not variables: its value is found once, the first time it is read --> example.sg:3:24 3 | static int Twice = n * 2 | ^ .fi .RE .SS "A static of a block that nothing uses" .RS 4 .nf void main() { static atomic int Calls = atomic 0 print("x") } .fi .RE .PP .RS 4 .nf error: `Calls` is never used --> example.sg:2:23 2 | static atomic int Calls = atomic 0 | ^^^^^ hint: take the line away: a static of a block is seen only by the lines after it in the block .fi .RE .SS "A method that changes it" .RS 4 .nf static List Names = List { "a" } void main() { Names.add("b") print(Names) } .fi .RE .PP .RS 4 .nf error: `add` changes the list, but it is reached through the static `Names` a static that changes is `static atomic`, changed by its atomic methods, or `static locked`, changed inside a `lock` block --> example.sg:4:5 4 | Names.add("b") | ^^^^^ .fi .RE .SS "A local with the name of a static" .RS 4 .nf static int Limit = 3 void main() { var Limit = 4 print(Limit) } .fi .RE .PP .RS 4 .nf error: `Limit` is the name of a static, and a local cannot take it .fi .RE .SH "SEE ALSO" .BR sg-const (7), .BR sg-atomic (7), .BR sg-shared (7), .BR sg-lock (7), .BR sg-public (7) .PP https://sg\-lang.org/reference/static.html