.\" Made by website/plain/build.mjs from website/words/var.md; change that file. .TH SG-VAR 7 2026-11-28 "SG" "SG reference" .SH NAME var \- declares a variable; its type comes from its value .SH SYNOPSIS .TP \fBvar name = value\fR a variable of the type of its value .TP \fBT name = value\fR the same, with the type written .TP \fBvar x = read place\fR a variable that borrows, to look .TP \fBvar x = ref place\fR a variable that borrows, to change .TP \fBvar other => ...\fR in a match, an arm that takes the value under a name .TP \fB_ = value\fR no variable: the value is dropped on purpose .SH DESCRIPTION \fBvar\fR declares a variable and gives it a value in one line; the type is the type of the value. A whole number that fits is an \fBint\fR and a larger one a \fBlong\fR, a number with a point a \fBdouble\fR, \fB"text"\fR a \fBstring\fR, \fB\&'c'\fR a \fBchar\fR\&. Write the type in place of \fBvar\fR when the value does not say it: \fBlong total = 0\fR\&. .PP Every variable has a value from the start and can be changed, but it keeps its type. Every variable must be read somewhere, since one that is not is a leftover or a mistake. A name means one thing in a function: a block inside cannot declare a variable with the name of one around it. .PP A variable owns its value, unless its value is a borrow: \fBvar x = read place\fR and \fBvar x = ref place\fR keep a borrow, which lasts until the last use of \fBx\fR\&. A borrowed number used as a value is copied: \fBvar before = n\fR, with \fBn\fR a \fBref int\fR, is a number of its own. .SH EXAMPLES .SS "The type from the value" .RS 4 .nf void main() { var count = 3 var ratio = 0.5 var name = "Pasha" var letter = 'P' var ready = true count += 1 print($"{count} {ratio} {name} {letter} {ready}") } .fi .RE .PP .RS 4 .nf 4 0.5 Pasha P true .fi .RE .PP \fBcount\fR is an \fBint\fR, \fBratio\fR a \fBdouble\fR, \fBname\fR a \fBstring\fR, \fBletter\fR a \fBchar\fR and \fBready\fR a \fBbool\fR\&. .SS "The type written" .RS 4 .nf void main() { long total = 0 byte small = 200 double half = 1 var big = 3000000000 total += 5 print($"{total} {small} {half} {big}") } .fi .RE .PP .RS 4 .nf 5 200 1 3000000000 .fi .RE .PP \fB0\fR alone would be an \fBint\fR; \fBlong total = 0\fR makes it a \fBlong\fR\&. \fBbig\fR is a \fBlong\fR with no type written, since the number does not fit in an \fBint\fR\&. .SS "A variable that borrows" .RS 4 .nf void main() { var counts = List { 1, 2, 3 } var slot = ref counts[1] slot += 40 print(counts) } .fi .RE .PP .RS 4 .nf [1, 42, 3] .fi .RE .PP Writing to \fBslot\fR writes to the item it borrows. While \fBslot\fR is used, \fBcounts\fR cannot be touched in any other way. .SS "A borrowed number is copied" .RS 4 .nf void bump(ref int n) { var before = n n += 1 print($"{before} -> {n}") } void main() { var hits = 7 bump(ref hits) print(hits) } .fi .RE .PP .RS 4 .nf 7 -> 8 8 .fi .RE .PP \fBbefore\fR keeps the number as it was. A second borrow would be written \fBvar other = ref n\fR\&. .SS "A name in a match" .RS 4 .nf string describe(int code) { return match code { 200 => "fine" 404 => "not found" var other => $"code {other}".toString() } } void main() { print(describe(200)) print(describe(418)) } .fi .RE .PP .RS 4 .nf fine code 418 .fi .RE .PP \fBvar other\fR takes every value the arms above did not, under a name. .SH "WHAT THE COMPILER REFUSES" .SS "A value of another type" .RS 4 .nf void main() { var count = 3 count = 1.5 } .fi .RE .PP .RS 4 .nf error: expected `int`, found `double` --> example.sg:3:13 3 | count = 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 variable nothing reads" .RS 4 .nf void main() { var count = 3 int limit = 10 print(limit) } .fi .RE .PP .RS 4 .nf error: `count` is never used --> example.sg:2:9 2 | var count = 3 | ^^^^^ hint: take the line away; to run what it is given and drop the value on purpose, write `_ = ...` .fi .RE .SS "A name that hides another" .RS 4 .nf void main() { var count = 3 if count > 2 { var count = 4 print(count) } } .fi .RE .PP .RS 4 .nf error: `count` is already a local variable of a block around this one, and a new name cannot hide it .fi .RE .SS "A value whose type cannot be found" .RS 4 .nf void main() { var result = Ok(1) print(result ?? 0) } .fi .RE .PP .RS 4 .nf error: the type of this `Result` cannot be found from `Ok(...)` alone --> example.sg:2:18 2 | var result = Ok(1) | ^^^^^ hint: write the type where the value goes, for example `Result x = Ok(...)` .fi .RE .PP Nothing in \fBOk(1)\fR says what the error would be. A line is read on its own, so the type is written there: \fBResult result = Ok(1)\fR\&. .SH "SEE ALSO" .BR sg-read (7), .BR sg-ref (7), .BR sg-match (7), .BR sg-const (7) .PP https://sg\-lang.org/reference/var.html