.\" Made by website/plain/build.mjs from website/words/assert.md; change that file. .TH SG-ASSERT 7 2026-11-28 "SG" "SG reference" .SH NAME assert \- stops the program, or fails the test, when a condition is false .SH SYNOPSIS .TP \fBassert(condition)\fR panics when the bool is false; the text of the condition is the message .TP \fBassert(a == b), !=, <, <=, >, >=\fR the same; a failed comparison writes the value of each side too .TP \fBconst assert(condition)\fR checked when the program is compiled; a false one is a compile error .TP \fBconst assert(condition, "message")\fR the same, with a message for the error .SH DESCRIPTION \fBassert\fR takes one \fBbool\fR\&. When it is true, nothing happens. When it is false, \fBassert\fR panics: in a program, the program stops with exit code 1; in a test, that test fails and the other tests still run. The message is \fBassert failed:\fR and the condition as it is written in the source. When the condition is one comparison, the value of each side follows, as \fBleft:\fR and \fBright:\fR; a list, a struct or an enum is written whole, field by field. A condition joined with \fB&&\fR or \fB||\fR is written with no values. .PP \fBassert\fR is on in every build, a release build too; there is no switch that leaves it out. It has one form: there is no \fBassertEqual\fR, and no message, since the condition written is the message. A check that needs a message of its own is an \fBif\fR with a \fBpanic("...")\fR\&. \fBassert\fR only looks at what it is given: a value in the condition is not moved, and can be used after it. .PP \fBassert\fR does not narrow. After \fBassert(name is not null)\fR, \fBname\fR is still a \fBT?\fR, and after \fBassert(result is Ok(n))\fR, \fBn\fR is not seen. To check and keep the value in one step, write \fBname ?? panic("...")\fR\&. .PP \fBconst assert\fR is checked by the compiler, not when the program runs. It goes at the top of a file or in a function, and its condition is made as a const is, with no call; \fBMemory.sizeOf()\fR and \fBMemory.alignOf()\fR are allowed there, and are measured for the machine the program is compiled for. A false one stops the build with the message, or the condition, and the two sides of a comparison. .SH EXAMPLES .SS "In a program" .RS 4 .nf void main() { var totalCents = 1000 var people = 3 var each = totalCents / people var left = totalCents % people assert(each * people + left == totalCents) print($"{each} each, {left} left over") } .fi .RE .PP .RS 4 .nf 333 each, 1 left over .fi .RE .PP The condition holds, so \fBassert\fR does nothing and the program goes on. Had the split lost a cent, the program would stop at that line. .SS "In a test" .RS 4 .nf string initials(read string name) { var result = "" var next = true for c in name.chars() { if c == ' ' { next = true } else if next { result.write(c) next = false } } return result } test "one letter for each word" { assert(initials("Ada Lovelace") == "AL") assert(initials("Grace Brewster Hopper") == "GBH") } test "spaces in a row are passed over" { var text = initials(" Alan Turing ") assert(text.length() == 2) assert(text != "") } .fi .RE .PP .RS 4 .nf ok one letter for each word ok spaces in a row are passed over 2 test(s) passed .fi .RE .PP This is what \fBsg test example.sg\fR prints. A false \fBassert\fR would print \fBFAIL\fR and the test's name, then the same message it writes in a program; see [\fBtest\fR](test.md). .SS "Comparing structs" .RS 4 .nf struct Money : Copyable, Equatable, Formattable { long cents char currency } Money total(read List prices) { long sum = 0 for price in read prices { assert(price.currency == prices[0].currency) sum += price.cents } return Money { cents = sum, currency = prices[0].currency } } void main() { var prices = List { Money { cents = 450, currency = 'E' }, Money { cents = 1200, currency = 'E' } } var sum = total(read prices) assert(sum == Money { cents = 1650, currency = 'E' }) print(sum) } .fi .RE .PP .RS 4 .nf Money { cents = 1650, currency = 'E' } .fi .RE .PP \fB==\fR on a struct needs \fBEquatable\fR\&. The first \fBassert\fR keeps \fBtotal\fR from adding cents of two currencies; it checks each price as the loop reaches it. .SS "An is in the condition" .RS 4 .nf void main() { var port = int.parse("8080") assert(port is Ok(n) && n > 0 && n < 65536) var value = port ?? panic("checked above") print(value + 1) } .fi .RE .PP .RS 4 .nf 8081 .fi .RE .PP \fBn\fR is seen only inside the condition. After the \fBassert\fR, \fBport\fR is still a \fBResult\fR, so \fB??\fR takes the value out; its \fBpanic\fR would run only if the \fBassert\fR above had not. .SS "Checked when the program is compiled" .RS 4 .nf packed struct Header { byte kind uint length } const assert(Memory.sizeOf
() == 5, "a header is five bytes on the disk") void main() { var header = Header { kind = 1, length = 300 } print(header.length) } .fi .RE .PP .RS 4 .nf 300 .fi .RE .PP The size of \fBHeader\fR is known before the program runs, so the check costs nothing when it runs. Without \fBpacked\fR, the struct takes eight bytes and the build stops; see the last case below. .SH "WHAT STOPS THE PROGRAM" .SS "A comparison of two structs" .RS 4 .nf struct Money : Copyable, Equatable { long cents char currency } struct Line : Equatable { string item int count Money price } Line lineOf(string item, int count, long cents) { return Line { item = item, count = count, price = Money { cents = cents * count, currency = 'E' } } } void main() { var line = lineOf("tea", 2, 450) var expected = Line { item = "tea", count = 2, price = Money { cents = 950, currency = 'E' } } assert(line == expected) } .fi .RE .PP .RS 4 .nf panic: assert failed: line == expected left: Line { item = "tea", count = 2, price = Money { cents = 900, currency = 'E' } } right: Line { item = "tea", count = 2, price = Money { cents = 950, currency = 'E' } } --> example.sg:19:5 19 | assert(line == expected) | ^^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP Each side is written field by field, the struct inside it too, so the field that differs can be found: \fBcents\fR is \fB900\fR on the left. A compiled program writes the same lines. .SS "A condition that is not one comparison" .RS 4 .nf void main() { var stock = 3 var wanted = 5 assert(wanted > 0 && stock >= wanted) print("shipped") } .fi .RE .PP .RS 4 .nf panic: assert failed: wanted > 0 && stock >= wanted --> example.sg:4:5 4 | assert(wanted > 0 && stock >= wanted) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP With \fB&&\fR, only the condition is written. Two \fBassert\fRs, \fBassert(wanted > 0)\fR and \fBassert(stock >= wanted)\fR, would each write their two sides. .SH "WHAT THE COMPILER REFUSES" .SS "A message" .RS 4 .nf void main() { var stock = 3 assert(stock >= 5, "not enough in stock") } .fi .RE .PP .RS 4 .nf error: `assert` takes 1 argument, 2 given `assert` writes the condition as its message --> example.sg:3:5 3 | assert(stock >= 5, "not enough in stock") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ hint: for a message of your own: `if !(stock >= 5) { panic("not enough in stock") }` .fi .RE .PP The condition is the message. For a text of your own, the hint turns the condition around and gives the text to \fBpanic\fR; \fBif stock < 5 { panic("not enough in stock") }\fR says the same. Only \fBconst assert\fR takes a message. .SS "A number for a condition" .RS 4 .nf int stockOf(read string code) { return code == "K-1" ? 3 : 0 } void main() { var stock = stockOf("K-1") assert(stock) } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `int` --> example.sg:7:12 7 | assert(stock) | ^^^^^ hint: a number is not a condition; compare it: `stock != 0` .fi .RE .SS "A value used as if assert had narrowed it" .RS 4 .nf string? homeOf(read string user) { if user == "ada" { return "/home/ada" } return null } void main() { var home = homeOf("ada") assert(home is not null) print(home.length()) } .fi .RE .PP .RS 4 .nf error: this is `string?` and may be `null`; `.length()` needs a value --> example.sg:11:11 11 | print(home.length()) | ^^^^ hint: test it first: `if home is not null { ... }` .fi .RE .PP \fBvar home = homeOf("ada") ?? panic("ada has a home")\fR checks the same and gives a \fBstring\fR\&. .SS "A const assert that is false" .RS 4 .nf struct Header { byte kind uint length } const assert(Memory.sizeOf
() == 5, "a header is five bytes on the disk") void main() { var header = Header { kind = 1, length = 300 } print(header.length) } .fi .RE .PP .RS 4 .nf error: const assert failed: a header is five bytes on the disk left: 8 right: 5 --> example.sg:6:14 6 | const assert(Memory.sizeOf
() == 5, "a header is five bytes on the disk") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP Without \fBpacked\fR, three bytes of padding keep \fBlength\fR at a multiple of four. .SH "SEE ALSO" .BR sg-test (7), .BR sg-test (1), .BR sg-const (7), .BR sg-is (7) .PP https://sg\-lang.org/reference/assert.html