.\" Made by website/plain/build.mjs from website/words/range.md; change that file. .TH SG-RANGE 7 2026-11-28 "SG" "SG reference" .SH NAME a..b \- a range: the numbers from a up to b, without b; in a pattern, a..=b takes b too .SH SYNOPSIS .TP \fBfor i in a..b\fR the numbers from a up to b, without b .TP \fBfor i in (a..b).reversed()\fR the same numbers, the last first .TP \fBxs[a..b], text[a..b]\fR the items or the bytes from a up to b, without b, as a slice or a read string .TP \fBxs[..b], xs[a..], xs[..]\fR from the start, to the end, the whole .TP \fBxs[1..^1], xs[^2..]\fR an end counted from the end: see [xs[^1]](index-from-end.md) .TP \fBref xs[a..b]\fR a part of a list, an array or a slice, to write .TP \fB\&'a'..='z', 0..=9, \-5..=\-1\fR in a pattern: from the first value to the last, both taken .SH DESCRIPTION \fBa..b\fR is the numbers from \fBa\fR up to \fBb\fR\&. The end is not one of them: \fB0..3\fR is 0, 1 and 2, so \fB0..n\fR has \fBn\fR numbers and \fB0..xs.length()\fR is every position of \fBxs\fR\&. A range is not a value of its own. It is written in three places: at the head of a \fBfor\fR, between the brackets of an index, and in a pattern. .PP In a \fBfor\fR, the ends are integers and are found once, the left first, before the first round. The count has the type the two ends share: \fB0..5\fR counts in \fBint\fR, \fB0..xs.length()\fR in \fBnint\fR, a \fBbyte\fR start and a constant end in \fBbyte\fR\&. Two ends whose types do not mix (\fBint\fR and \fBnuint\fR) are an error. A range whose end is not above its start gives no round. \fB(a..b).reversed()\fR goes over the same numbers from \fBb \- 1\fR down to \fBa\fR\&. .PP Between the brackets of an index, a range takes a part of a list, an array, a slice or a text, and is a borrow: a \fBread T[]\fR, or a \fBread string\fR of a text's bytes. \fBref xs[a..b]\fR lends the part to write. Either end may be left out, and either may count from the end with \fB^\fR\&. The ends take any integer that widens to \fBnint\fR\&. They are checked against the length when the program runs, in every build: an end outside, an end before the start, or a cut through a character of a text stops the program. A range that is empty, as \fBxs[2..2]\fR or \fBxs[3..]\fR on three items, is not a mistake. .PP In a pattern a range is written \fBa..=b\fR and takes \fBb\fR too, so that \fB\&'a'..='z'\fR has the \fBz\fR\&. Its ends are numbers or characters as written, the first not greater than the last. \fB\&..=\fR is written only in a pattern, and \fB\&..\fR is an error there. A \fBfor\fR that needs its end too goes to \fBb + 1\fR\&. .SH EXAMPLES .SS "Counting" .RS 4 .nf void main() { for i in 1..4 { print(i) } var total = 0 for _ in 0..5 { total += 2 } print(total) for i in 3..0 { print(i) } } .fi .RE .PP .RS 4 .nf 1 2 3 10 .fi .RE .PP \fB1..4\fR is 1, 2 and 3. \fB0..5\fR is five rounds; \fB_\fR is the name when the body does not need the count. \fB3..0\fR ends before it starts, so its loop runs no round. .SS "A part of a list, an array or a text" .RS 4 .nf void main() { var xs = List { 10, 20, 30, 40, 50 } print(xs[1..3]) print(xs[..2]) print(xs[3..]) print(xs[..]) var primes = int[4] { 2, 3, 5, 7 } print(primes[1..3]) var line = "key=value" var at = line.indexOf('=') ?? 0 print(line[..at]) print(line[at + 1..]) } .fi .RE .PP .RS 4 .nf [20, 30] [10, 20] [40, 50] [10, 20, 30, 40, 50] [3, 5] key value .fi .RE .PP \fBxs[1..3]\fR is the items at 1 and 2. A range of a text counts bytes, as \fBindexOf\fR does, and is a \fBread string\fR that looks at the text: nothing is copied. .SS "Empty ranges" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } for n in 0..xs.length() + 1 { print($"{n}: {xs[n..]} {xs[..n]}") } var text = "abc" print(text[1..1].length()) } .fi .RE .PP .RS 4 .nf 0: [10, 20, 30] [] 1: [20, 30] [10] 2: [30] [10, 20] 3: [] [10, 20, 30] 0 .fi .RE .PP \fBxs[3..]\fR on three items is empty, and so is \fBxs[..0]\fR: an end may be the length itself, one past the last item. \fBn\fR is a \fBnint\fR, since \fBxs.length()\fR is one. .SS "The type of the count" .RS 4 .nf void main() { byte first = 250 for b in first..253 { print(b) } for big in 4999999999..5000000001 { print(big) } var all = List() for i in 0..256 { all.add((byte)i) } print($"{all.length()} {all[0]} {all[^1]}") } .fi .RE .PP .RS 4 .nf 250 251 252 4999999999 5000000000 256 0 255 .fi .RE .PP \fBb\fR is a \fBbyte\fR, the type of \fBfirst\fR, and \fBbig\fR a \fBlong\fR, since its ends do not fit an \fBint\fR\&. A count of a type never reaches past the type: a range stops before its end, and 256 is not a \fBbyte\fR\&. To go over every value of a \fBbyte\fR, count in \fBint\fR and convert. .SS "Ranges in patterns" .RS 4 .nf string kind(char c) { return match c { 'a'..='z' or 'A'..='Z' => "letter" '0'..='9' => "digit" _ => "other" } } string size(int n) { return match n { -5..=-1 => "a little below" 0 => "none" 1..=9 => "a few" 10..=99 => "some" _ => "far" } } void main() { for c in "aZ7?".chars() { print($"{c} {kind(c)}") } print($"{size(-3)} {size(0)} {size(9)} {size(10)} {size(100)}") } .fi .RE .PP .RS 4 .nf a letter Z letter 7 digit ? other a little below none a few some far .fi .RE .PP A pattern range takes both ends: \fB1..=9\fR has the 9. Ranges and values join with \fBor\fR\&. .SS "Counting down" .RS 4 .nf void main() { var xs = List { "a", "b", "c" } for i in (0..xs.length()).reversed() { print($"{i} {xs[i]}") } for i in (5..5).reversed() { print(i) } } .fi .RE .PP .RS 4 .nf 2 c 1 b 0 a .fi .RE .PP \fB(0..3).reversed()\fR is 2, 1 and 0. The count never goes below the start, so a count of an unsigned type, as \fBuint\fR, stops at 0 without wrapping. \fB(5..5).reversed()\fR has no number in it. A range in brackets is written only so, at the head of a \fBfor\fR\&. .SH "WHAT STOPS THE PROGRAM" .SS "A range that ends before it starts" .RS 4 .nf void main() { var xs = List { 10, 20, 30, 40 } var from = 3 print(xs[from..1]) } .fi .RE .PP .RS 4 .nf panic: range 3..1 ends before it starts .fi .RE .PP In a \fBfor\fR, such a range runs no round. Between brackets it is a mistake in the data, not a request for nothing. .SS "An end past the items" .RS 4 .nf void main() { var xs = List { 10, 20, 30, 40 } var from = 9 print(xs[from..]) } .fi .RE .PP .RS 4 .nf panic: range 9..4 is out of range: the length is 4 .fi .RE .PP \fBxs[from..]\fR ends at the length, 4, so the message shows \fB9..4\fR\&. .SS "A cut through a character" .RS 4 .nf void main() { var word = "año" print(word[0..2]) } .fi .RE .PP .RS 4 .nf panic: range 0..2 cuts a character in half .fi .RE .PP \fBñ\fR is bytes 1 and 2 of the text, and a \fBstring\fR is always valid UTF-8. .SS "An end that overflows" .RS 4 .nf void main() { byte last = 255 for b in (byte)250..last + 1 { print(b) } } .fi .RE .PP .RS 4 .nf panic: overflow: 255 + 1 does not fit in byte .fi .RE .PP An end is a sum like any other: in a debug build and in the interpreter its overflow stops the program, and in a release build it wraps, so this loop runs no round there. The count is a \fBbyte\fR, which never holds 256: count in \fBint\fR instead. .SH "WHAT THE COMPILER REFUSES" .SS "\&..= outside a pattern" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } print(xs[0..=1]) } .fi .RE .PP .RS 4 .nf error: `..=` is written only in a pattern; a range of a text or a slice stops before its end --> example.sg:3:15 3 | print(xs[0..=1]) | ^^^ hint: `[a..b + 1]` .fi .RE .PP A \fBfor\fR refuses \fB1..=3\fR the same way, with the hint \fBfor i in a..b + 1\fR\&. .SS "\&.. in a pattern" .RS 4 .nf string kind(char c) { return match c { 'a'..'z' => "small" _ => "other" } } void main() { print(kind('q')) } .fi .RE .PP .RS 4 .nf error: a range in a pattern takes its last value too, and is written with `..=` --> example.sg:3:12 3 | 'a'..'z' => "small" | ^^ hint: `'a'..='z'` .fi .RE .PP With \fB\&..\fR, \fB\&'a'..'z'\fR would quietly leave out the \fBz\fR\&. .SS "A range that takes nothing" .RS 4 .nf string size(int n) { return match n { 9..=1 => "a few" _ => "other" } } void main() { print(size(4)) } .fi .RE .PP .RS 4 .nf error: this range takes nothing: its first value is greater than its last --> example.sg:3:9 3 | 9..=1 => "a few" | ^^^^^ hint: the smaller value comes first: `1..=9` .fi .RE .SS "A name at the end of a pattern range" .RS 4 .nf const int Top = 9 string size(int n) { return match n { 1..=Top => "a few" _ => "other" } } void main() { print(size(4)) } .fi .RE .PP .RS 4 .nf error: a range in a pattern ends with a number or a character --> example.sg:5:13 5 | 1..=Top => "a few" | ^^^ .fi .RE .PP The ends of a pattern are written as they are, so that the compiler can count what the arms take. A constant such as \fBint.MaxValue\fR is refused the same way, with a note that it is not an end of a range yet. .SS "A range kept in a variable" .RS 4 .nf void main() { var firstThree = 0..3 for i in firstThree { print(i) } } .fi .RE .PP .RS 4 .nf error: expected end of line, found `..` a range is not a value: it is written at the head of a `for` (`for i in 0..5`) or between the brackets of an index (`xs[1..3]`) --> example.sg:2:23 2 | var firstThree = 0..3 | ^^ .fi .RE .PP A range is not a value: it is written where it is used, at the head of the \fBfor\fR\&. .SS "reversed on a part of a list" .RS 4 .nf void main() { var xs = List { 1, 2, 3 } for x in xs[1..].reversed() { print(x) } } .fi .RE .PP .RS 4 .nf error: `int[]` has no method `reversed` --> example.sg:3:22 3 | for x in xs[1..].reversed() { | ^^^^^^^^ hint: `reversed` goes over a range from its end: `for i in (0..xs[1..].length()).reversed()`; `xs[1..].reverse()` turns the items round in place .fi .RE .PP \fB(a..b).reversed()\fR is written at the head of a \fBfor\fR, on a range of numbers. A list, an array and a slice have \fBreverse\fR, which changes the order of their items. .SS "A range of characters" .RS 4 .nf void main() { for c in 'a'..'e' { print(c) } } .fi .RE .PP .RS 4 .nf error: the start of the range must be an integer, found `char` --> example.sg:2:14 2 | for c in 'a'..'e' { | ^^^ hint: a `for` counts integers: `for c in (int)'a'..(int)'e'`, and `(char)c` is the character .fi .RE .PP A \fBfor\fR counts integers: the codes of the letters, made characters again with \fB(char)\fR\&. The characters of a text are gone over with \fB"abcd".chars()\fR\&. .SS "Ends whose types do not mix" .RS 4 .nf void main() { int from = -2 nuint to = 3 for i in from..to { print(i) } } .fi .RE .PP .RS 4 .nf error: `int` and `nuint` do not mix: neither holds every value of the other --> example.sg:4:14 4 | for i in from..to { | ^^^^^^^^ hint: convert one side to the type of the other: `(int)to`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SH "SEE ALSO" .BR sg-for (7), .BR sg-slice (7), .BR sg-index-from-end (7), .BR sg-match (7), .BR sg-or (7), .BR sg-string (7) .PP https://sg\-lang.org/reference/range.html