.\" Made by website/plain/build.mjs from website/words/enum.md; change that file. .TH SG-ENUM 7 2026-11-28 "SG" "SG reference" .SH NAME enum \- declares a type whose value is one of several variants, each with values of its own .SH SYNOPSIS .TP \fBenum Name { A ... }\fR a type with one variant on each line .TP \fBCircle(int radius)\fR a variant that carries values, named and typed like parameters .TP \fBName.A, Name.Circle(2)\fR a value of a variant, built with the enum's name in front .TP \fBenum Name : Copyable, Formattable\fR an enum that takes built-in interfaces .TP \fBT m(read this), ref this, this\fR a method, written after or between the variants .TP \fBenum Name : byte { A = 1 ... }\fR an enum with numbers: an integer type first, a number for each variant .TP \fB(byte)value, Name.fromValue(n)\fR the number of a variant; the variant of a number, or null .TP \fBnew Name.Node(...), Name* field\fR a variant on the heap, for an enum that holds itself .SH DESCRIPTION An enum lists its variants, one per line, with no commas. A value of the enum is always exactly one of them. A variant may carry values, written like the parameters of a function: \fBRect(int width, int height)\fR\&. It is built with the enum's name in front, \fBShape.Rect(3, 4)\fR, and every value it carries must be given. .PP \fBmatch\fR looks inside an enum and must have an arm for every variant, so a variant added later shows each place that has to handle it. \fBis\fR tests for one variant in a condition. In a pattern the variant is written without the enum's name: \fBRect(w, h)\fR\&. .PP An enum takes the room of its largest variant plus a small tag that says which one it holds. It lives where its variable is, with no allocation. So an enum cannot hold itself directly: a variant that carries the same enum points to it, \fBExpr* left\fR, or holds it in a \fBList\fR\&. .PP An enum has one owner and is moved like a struct, unless it is marked \fBCopyable\fR\&. It is printed only when it is \fBFormattable\fR, and compared with \fB==\fR only when it is \fBEquatable\fR\&. An enum may have methods, as a struct does, but no fields: a value every variant needs is written in each variant. .PP An enum with numbers names its integer type first, \fBenum Block : byte\fR\&. Its variants carry nothing, and each stands for a number: the one written, or one more than the variant above. A cast gives the number, and \fBfromValue\fR goes the other way. .SH EXAMPLES .SS "Plain variants" .RS 4 .nf enum Light : Copyable, Formattable, Equatable { Red Yellow Green } void main() { var light = Light.Red var before = light light = Light.Green print($"{before} then {light}") print(light == Light.Green) } .fi .RE .PP .RS 4 .nf Red then Green true .fi .RE .PP \fBCopyable\fR makes \fBvar before = light\fR a copy, so both can be used. A variant is printed by its name alone. .SS "Variants that carry values" .RS 4 .nf enum Reply : Formattable { Found(string name, int age) Missing(string key) Busy } Reply lookup(read string key) { if key == "ada" { return Reply.Found("Ada", 36) } if key == "" { return Reply.Busy } return Reply.Missing(key.clone()) } void main() { var keys = List { "ada", "bob", "" } for key in read keys { match lookup(key) { Found(name, age) => print($"{name} is {age}") Missing(k) => print($"no one is called {k}") Busy => print("try later") } } print(lookup("ada")) } .fi .RE .PP .RS 4 .nf Ada is 36 no one is called bob try later Found("Ada", 36) .fi .RE .PP Each arm names the values its variant carries. \fBBusy\fR carries none, and is written without parentheses. .SS "Methods" .RS 4 .nf enum Shape : Formattable { Circle(int radius) Rect(int width, int height) Empty Shape square(int side) { return Shape.Rect(side, side) } int area(read this) { return match this { Circle(r) => 3 * r * r Rect(w, h) => w * h Empty => 0 } } void grow(ref this, int by) { match this { Circle(r) => r += by Rect(w, h) => { w += by h += by } Empty => {} } } } void main() { var shapes = List { Shape.Circle(2), Shape.square(3), Shape.Empty } for shape in ref shapes { shape.grow(1) } for shape in read shapes { print($"{shape}: {shape.area()}") } } .fi .RE .PP .RS 4 .nf Circle(3): 27 Rect(4, 4): 16 Empty: 0 .fi .RE .PP Methods follow the variants. \fBsquare\fR has no \fBthis\fR and is called on the type. In \fBgrow\fR the \fBmatch\fR is on a \fBref\fR borrow, so \fBr\fR, \fBw\fR and \fBh\fR borrow the values inside, and changing them changes the shape. .SS "is: one variant" .RS 4 .nf enum Reply : Formattable { Found(string name, int age) Missing(string key) Busy } void main() { var reply = Reply.Found("Ada", 36) if reply is Found(name, age) && age > 30 { print($"{name}, over thirty") } if reply is not Busy { print(reply) } } .fi .RE .PP .RS 4 .nf Ada, over thirty Found("Ada", 36) .fi .RE .PP \fBis\fR only looks: \fBreply\fR is still there after it. The names it gives are seen in the block and along \fB&&\fR\&. .SS "Numbers" .RS 4 .nf enum Block : byte, Copyable, Formattable { Extension = 0x21 Image = 0x2C Trailer = 0x3B } enum Level : int, Copyable, Comparable, Formattable { Low = 10 Middle High = 30 } string describe(byte code) { return match Block.fromValue(code) { null => $"no block starts with {code:x2}".toString() var block => $"{block} starts with {(byte)block:x2}".toString() } } void main() { print(describe(0x2C)) print(describe(0x00)) print((int)Level.Middle) var levels = List { Level.High, Level.Low, Level.Middle } levels.sort() print(levels) } .fi .RE .PP .RS 4 .nf Image starts with 2c no block starts with 00 11 [Low, Middle, High] .fi .RE .PP \fBMiddle\fR is one more than \fBLow\fR\&. A byte read from a file may be no variant at all, so \fBfromValue\fR gives a \fBBlock?\fR\&. \fBComparable\fR orders the variants by their numbers. .SS "An enum that holds itself" .RS 4 .nf enum Json : Formattable { Number(double value) Text(string value) Items(List items) Nothing } int depth(read Json value) { return match value { Items(items) => { var deepest = 0 for item in items { deepest = deepest.max(depth(item)) } deepest + 1 } _ => 0 } } void main() { var inner = Json.Items(List { Json.Text("a") }) var outer = Json.Items(List { Json.Number(1), inner, Json.Nothing }) print(outer) print(depth(read outer)) } .fi .RE .PP .RS 4 .nf Items([Number(1), Items([Text("a")]), Nothing]) 2 .fi .RE .PP A \fBList\fR keeps its items on the heap, so a \fBJson\fR that holds a list of \fBJson\fR still has a size. \fBinner\fR is moved into the list. \fB_\fR takes every variant the arm above did not. .SH "WHAT THE COMPILER REFUSES" .SS "A variant without its values" .RS 4 .nf enum Shape { Circle(int radius) Empty } void main() { var shape = Shape.Circle } .fi .RE .PP .RS 4 .nf error: `Circle` carries 1 value(s) and cannot be built without them --> example.sg:7:17 7 | var shape = Shape.Circle | ^^^^^^^^^^^^ hint: `Shape.Circle(radius)` .fi .RE .SS "A field in an enum" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) string label } void main() { } .fi .RE .PP .RS 4 .nf error: an enum holds variants and functions, not fields --> example.sg:4:12 4 | string label | ^^^^^ hint: a value every variant carries is written in each variant: `Name(string label)` .fi .RE .SS "An enum inside itself" .RS 4 .nf enum Tree { Leaf(int value) Node(Tree left, Tree right) } void main() { } .fi .RE .PP .RS 4 .nf error: enum `Tree` contains itself, so it has no finite size --> example.sg:3:10 3 | Node(Tree left, Tree right) | ^^^^ hint: put it behind a pointer: `Tree* left` .fi .RE .PP \fBNode(Tree* left, Tree* right)\fR keeps each side on the heap, built with \fBnew Tree.Leaf(1)\fR\&. .SS "A number from an enum without numbers" .RS 4 .nf enum Light { Red Green } void main() { print((int)Light.Green) } .fi .RE .PP .RS 4 .nf error: a `Light` value cannot be converted to `int` the variants of `Light` have no numbers; an enum has them when it names their integer type: `enum Light : int` --> example.sg:7:11 7 | print((int)Light.Green) | ^^^^^^^^^^^^^^^^ .fi .RE .PP The order of the lines is not a number. With \fBenum Light : int\fR, each variant has a number that stays the same when the lines are moved, so what a file or a protocol stored keeps its meaning. .SH "SEE ALSO" .BR sg-match (7), .BR sg-is (7), .BR sg-struct (7), .BR sg-new (7), .BR sg-interface (7) .PP https://sg\-lang.org/reference/enum.html