.\" Made by website/plain/build.mjs from website/words/list.md; change that file. .TH SG-LIST 7 2026-11-28 "SG" "SG reference" .SH NAME List \- a list of items that grows, kept in one block of memory .SH SYNOPSIS .TP \fBList()\fR an empty list .TP \fBList { a, b }\fR a list with these items .TP \fBxs.add(item)\fR adds the item at the end .TP \fBxs[i], xs[^1]\fR the item at i, the last item .TP \fBxs.length()\fR the number of items .TP \fBfor x in xs\fR each item in turn .TP \fBread T[], ref T[]\fR a function that takes any list or array .TP \fBList\fR a list of borrows, which copies nothing .SH DESCRIPTION A \fBList\fR owns its items, one after another in one block of memory, and takes a bigger block when it grows. When the list ends, its items end with it. Reading outside the list stops the program with a panic, never reads another value's memory. .PP Every method is in Collections. A function that only needs the items takes \fBread T[]\fR or \fBref T[]\fR, and then works for an array and for a part of a list as well. .SH EXAMPLES .SS "Make one and add to it" .RS 4 .nf void main() { var names = List() names.add("Ada") names.add("Grace") print(names) print(names.length()) } .fi .RE .PP .RS 4 .nf ["Ada", "Grace"] 2 .fi .RE .SS "Read and write items" .RS 4 .nf void main() { var xs = List { 5, 3, 8 } print(xs[0]) print(xs[^1]) xs[1] += 10 print(xs) print(xs.get(9) ?? -1) } .fi .RE .PP .RS 4 .nf 5 8 [5, 13, 8] -1 .fi .RE .PP \fBxs[^1]\fR counts from the end. \fBget\fR gives \fBnull\fR outside the list instead of a panic, and \fB??\fR gives a value for that case. .SS "Give it to a function that only reads" .RS 4 .nf int total(read int[] values) { var sum = 0 for v in values { sum += v } return sum } void main() { var xs = List { 5, 3, 8, 1 } print(total(read xs)) print(total(xs[1..3])) } .fi .RE .PP .RS 4 .nf 17 11 .fi .RE .SS "Sort and search" .RS 4 .nf void main() { var xs = List { 5, 3, 8, 1 } xs.sort() print(xs) print(xs.binarySearch(8) ?? -1) print(xs.contains(4)) print(xs.removeLast() ?? -1) print(xs) } .fi .RE .PP .RS 4 .nf [1, 3, 5, 8] 3 false 8 [1, 3, 5] .fi .RE .SS "A list of borrows" .RS 4 .nf struct Order { string customer long amount } void main() { var orders = List { Order { customer = "ada", amount = 1200 }, Order { customer = "grace", amount = 300 }, Order { customer = "ada", amount = 80 } } var large = List() for o in read orders { if o.amount >= 100 { large.add(o) } } print($"{large.length()} {large[0].customer} {large[1].amount}") } .fi .RE .PP .RS 4 .nf 2 ada 300 .fi .RE .PP \fBlarge\fR points at orders that \fBorders\fR owns and copies none of them. While \fBlarge\fR is used, \fBorders\fR cannot be changed: that would leave \fBlarge\fR pointing at memory that moved. .SH "WHAT STOPS THE PROGRAM" .SS "An index outside the list" .RS 4 .nf void main() { var xs = List { 5, 3, 8 } print(xs[3]) } .fi .RE .PP .RS 4 .nf panic: index 3 is out of range: the length is 3 .fi .RE .SH "WHAT THE COMPILER REFUSES" .SS "Copying items that are not copied silently" .RS 4 .nf void main() { var names = List { "Ada".clone() } var more = List() more.addAll(read names) } .fi .RE .PP .RS 4 .nf error: `addAll` copies the items, and `string` is not `Copyable` --> example.sg:4:10 4 | more.addAll(read names) | ^^^^^^ hint: add them one by one, each a copy: `more.add(item.clone())` .fi .RE .PP A text owns its memory, so a copy of it is asked for with \fB\&.clone()\fR, where you can see it. .SH "SEE ALSO" .BR sg-read (7), .BR sg-ref (7) .PP https://sg\-lang.org/reference/list.html