.\" Made by website/plain/build.mjs from website/words/alias.md; change that file. .TH SG-ALIAS 7 2026-11-28 "SG" "SG reference" .SH NAME alias \- gives a type a second name: the same type, which builds what it builds .SH SYNOPSIS .TP \fBalias Scores = List\fR a second name of a type, at the top of a file .TP \fBpublic alias Stops = List\fR a name other modules can use too .TP \fBScores, read Scores, List, Scores?\fR it goes wherever a type goes .TP \fBalias P = Point, then P { x = 1, y = 2 }, P(1, 2), P.origin()\fR it builds what its type builds .TP \fBalias Big = long, then (Big)n, Big.MaxValue, Big.parse(text)\fR an alias of a built-in type casts and has its functions .TP \fBalias NodeRef = shared Node, then new NodeRef { ... }\fR new NodeRef is new shared Node .TP \fBalias Op = Func\fR the type of a function .TP \fBalias Link = NodeRef?\fR an alias in an alias .SH DESCRIPTION \fBalias Name = Type\fR gives a type a second name. It is not a new type but the same one, so values pass between the two names freely: a function that takes a \fBread Scores\fR is given a \fBList\fR\&. The alias is gone at compile time: a value prints as what its type prints, and a type the compiler names in a message is the type itself (\fBexpected `List`, found `string` \fR). .PP An alias builds what its type builds. For a struct that is \fBP { ... }\fR, \fBP(args)\fR when the struct has a \fBcreate\fR function, and its other functions, \fBP.origin()\fR; for an enum its variants, \fBWhy.Empty\fR; for a \fBshared\fR type, \fBnew NodeRef { ... }\fR or \fBnew NodeRef(args)\fR\&. A collection is the exception: it is built by the name of its type, \fBList { 1, 2 }\fR, not by its alias. In a pattern a variant is named alone, as always. An alias of a number type, \fBchar\fR, \fBbool\fR or \fBstring\fR is that type in a cast and in front of a dot: \fB(Big)n\fR, \fBCount.parse(text)\fR, \fBBig.MaxValue\fR, \fBText.join(", ", read parts)\fR\&. .PP An alias is written at the top level of a file, one on a line, and is seen by every file of its module, in any order. Like a type, it stays in its module unless it is written \fBpublic alias\fR, and a \fBpublic\fR alias cannot show a type that is not \fBpublic\fR\&. .PP An alias cannot be a borrow (\fBread Text\fR is written where the borrow is), cannot take type parameters, cannot take the name of another type, and cannot come back to itself, directly or through other aliases. \fBshared\fR cannot be put around an alias that is \fBshared\fR already. .PP \fBalias\fR is a word only at the top of a file, in front of a name and \fB=\fR\&. Elsewhere it is a name: a variable, a field or a function can be called \fBalias\fR\&. .SH EXAMPLES .SS "A shorter name for a collection" .RS 4 .nf alias Scores = List alias Board = HashMap int best(read Scores scores) { var top = 0 for score in scores { top = top.max(score) } return top } void main() { Board board = HashMap { } board.add("ada", List { 36, 41 }) board.add("grace", List { 45 }) List plain = List { 7, 3 } print(best(read plain)) var mine = board.get("ada") if mine is not null { print(best(mine)) } print(board.length()) } .fi .RE .PP .RS 4 .nf 7 41 2 .fi .RE .PP \fBbest\fR takes a \fBread Scores\fR and is given a \fBList\fR: they are one type. \fBBoard\fR names \fBScores\fR in its own type. A collection is still built by the name of its type, \fBHashMap { }\fR\&. .SS "A struct under a second name" .RS 4 .nf struct Point : Formattable, Equatable { int x int y Point create(int x, int y) { return Point { x = x, y = y } } Point origin() { return Point { x = 0, y = 0 } } } alias P = Point P mirror(read P p) { return P(p.y, p.x) } void main() { var a = P { x = 1, y = 2 } var b = P(3, 4) Point c = mirror(read b) print(a) print(c) print(P.origin() == Point.origin()) } .fi .RE .PP .RS 4 .nf Point { x = 1, y = 2 } Point { x = 4, y = 3 } true .fi .RE .PP \fBP { ... }\fR builds field by field, \fBP(3, 4)\fR calls \fBcreate\fR, and \fBP.origin()\fR is the function of \fBPoint\fR\&. The value prints as a \fBPoint\fR, since that is what it is. .SS "A built-in type under a second name" .RS 4 .nf alias Big = long alias Count = int alias Text = string void main() { int n = 5 Big total = (Big)n * 1000000000000 print(total) print(Big.MaxValue) print(Count.parse("12") ?? 0) var parts = List { "a", "b" } print(Text.join(", ", read parts)) } .fi .RE .PP .RS 4 .nf 5000000000000 9223372036854775807 12 a, b .fi .RE .PP \fB(Big)n\fR is \fB(long)n\fR, and \fBBig.MaxValue\fR is \fBlong.MaxValue\fR: the alias stands for its type there too. .SS "The type of a function" .RS 4 .nf alias Op = Func int add(int a, int b) { return a + b } int times(int a, int b) { return a * b } int fold(read List xs, Op op, int start) { var total = start for x in xs { total = op(total, x) } return total } void main() { var xs = List { 1, 2, 3, 4 } var ops = List { add, times } print(fold(read xs, ops[0], 0)) print(fold(read xs, ops[1], 1)) } .fi .RE .PP .RS 4 .nf 10 24 .fi .RE .PP \fBOp\fR is shorter than \fBFunc\fR in the signature of \fBfold\fR and in the list of operations. .SS "A shared node, and an alias in an alias" .RS 4 .nf struct Node { int value Link next } alias NodeRef = shared Node alias Link = NodeRef? int sum(read Link list) { var total = 0 Link at = share list while at is not null { total += at.value at = share at.next } return total } void main() { var last = new NodeRef { value = 2, next = null } Link first = new NodeRef { value = 1, next = share last } print(sum(read first)) print(sum(read last)) } .fi .RE .PP .RS 4 .nf 3 2 .fi .RE .PP \fBnew NodeRef { ... }\fR is \fBnew shared Node { ... }\fR, and \fBLink\fR is \fBshared Node?\fR\&. \fBNode\fR names \fBLink\fR before the line that declares it: the order of the lines does not matter. \fBlast\fR is a \fBNodeRef\fR, and is given where a \fBread Link\fR goes, as a \fBshared Node\fR is given where a \fBread shared Node?\fR goes. .SS "An enum and a Result" .RS 4 .nf enum Problem : Formattable { Empty NotANumber(string text) OutOfRange(int value) } alias Why = Problem alias Port = Result Port parsePort(read string text) { if text.length() == 0 { return Error(Why.Empty) } var n = int.parse(text) ?? return Error(Why.NotANumber(text.clone())) if n < 1 || n > 65535 { return Error(Why.OutOfRange(n)) } return Ok(n) } void main() { var texts = List { "8080", "", "http", "70000" } for text in read texts { match parsePort(text) { Ok(port) => print($"port {port}") Error(OutOfRange(n)) => print($"{n} is out of range") Error(why) => print(why) } } } .fi .RE .PP .RS 4 .nf port 8080 Empty NotANumber("http") 70000 is out of range .fi .RE .PP \fBWhy.Empty\fR builds a variant of \fBProblem\fR\&. In the arms of the \fBmatch\fR the variant is named alone, \fBOutOfRange(n)\fR, without \fBWhy\fR or \fBProblem\fR in front. .SS "A public alias, from another module" .RS 4 .nf trip/ main.sg route/ stop.sg .fi .RE .PP The file trip/route/stop.sg: .PP .RS 4 .nf public struct Stop : Formattable { public string name public int minutes } public alias Stops = List alias Minutes = int public Minutes total(read Stops stops) { Minutes sum = 0 for stop in stops { sum += stop.minutes } return sum } .fi .RE .PP The file trip/main.sg: .PP .RS 4 .nf use route void main() { Stops stops = List { Stop { name = "mill".clone(), minutes = 12 }, Stop { name = "bridge".clone(), minutes = 30 } } route.Stops same = stops print(same) int minutes = total(read same) print(minutes) } .fi .RE .PP .RS 4 .nf $ cd trip $ sg main.sg [Stop { name = "mill", minutes = 12 }, Stop { name = "bridge", minutes = 30 }] 42 .fi .RE .PP \fBStops\fR is public, so \fBmain.sg\fR names it, alone after \fBuse route\fR or in full, \fBroute.Stops\fR\&. \fBMinutes\fR stays inside \fBroute\fR; \fBtotal\fR may still give one, since a \fBMinutes\fR is an \fBint\fR, which every module names, and \fBmain.sg\fR takes it as an \fBint\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "A collection built by its alias" .RS 4 .nf alias Scores = List void main() { var scores = Scores() scores.add(3) print(scores) } .fi .RE .PP .RS 4 .nf error: `Scores` is `List`; a collection is built by the name of its type --> example.sg:4:18 4 | var scores = Scores() | ^^^^^^^^ hint: `List()` .fi .RE .PP The variable can still have the alias as its type: \fBScores scores = List()\fR\&. .SS "An alias of a borrow" .RS 4 .nf alias Text = read string void main() { print("hello") } .fi .RE .PP .RS 4 .nf error: an alias names a type that owns its value, not a borrow --> example.sg:1:14 1 | alias Text = read string | ^^^^^^^^^^^ hint: name the type alone and write `read` where the borrow is: `read Text` .fi .RE .SS "Aliases that name each other" .RS 4 .nf alias Tree = Forest alias Forest = List void main() { print("hello") } .fi .RE .PP .RS 4 .nf error: the aliases `Forest` and `Tree` name each other an alias is another name for a type that is written out in the end --> example.sg:2:16 2 | alias Forest = List | ^^^^^^^^^^ .fi .RE .PP A type that holds itself is a struct: \fBstruct Tree { List children }\fR\&. .SS "shared around a shared alias" .RS 4 .nf struct Node { int value } alias NodeRef = shared Node alias Again = shared NodeRef void main() { print("hello") } .fi .RE .PP .RS 4 .nf error: `NodeRef` is already `shared Node` --> example.sg:6:15 6 | alias Again = shared NodeRef | ^^^^^^^^^^^^^^ .fi .RE .SS "An alias inside a function" .RS 4 .nf void main() { alias Count = int Count n = 3 print(n) } .fi .RE .PP .RS 4 .nf error: there is no type named `alias` a type alias is written at the top of a file, outside every function: `alias X = int` --> example.sg:2:5 2 | alias Count = int | ^^^^^ .fi .RE .PP Inside a function, \fBalias\fR is a name, so the line reads as a variable \fBCount\fR of a type \fBalias\fR\&. .SS "The word using" .RS 4 .nf using Scores = List void main() { print("hello") } .fi .RE .PP .RS 4 .nf error: a type alias is written with `alias` --> example.sg:1:1 1 | using Scores = List | ^^^^^ hint: `alias Scores = List` .fi .RE .SS "A public alias that shows a private type" A file added to \fBtrip/route/\fR: .PP The file trip/route/plan.sg: .PP .RS 4 .nf struct Leg { int minutes } public alias Plan = List .fi .RE .PP .RS 4 .nf $ cd trip $ sg main.sg error: `Plan` is public, but it shows `Leg`, which is not --> route/plan.sg:5:14 5 | public alias Plan = List | ^^^^ `Leg` is defined here, without `public`: --> route/plan.sg:1:8 1 | struct Leg { | ^^^ hint: write `public` in front of `Leg`, or take it off `Plan` .fi .RE .PP Other modules could name \fBPlan\fR but not what it holds. .SH "SEE ALSO" .BR sg-public (7), .BR sg-use (7), .BR sg-shared (7), .BR sg-new (7), .BR sg-struct (7), .BR sg-enum (7) .PP https://sg\-lang.org/reference/alias.html