Range Literals

Range literals are an odd but useful feature in Conjure. By default, they are an abstract representation of a sequence of values, typically used in loops and slice operations. However, when assigned to a variable, they become a concrete iterator instance.

We can produce exclusive ranges (left inclusive, right exclusive) using .. and inclusive ranges (both ends inclusive) using ....

var exclusive = 0..5  // represents values 0, 1, 2, 3, 4
var inclusive = 0...5 // represents values 0, 1, 2, 3, 4, 5

Ranges are commonly used with loops and slice operations. The left and right sides of a range can be any integer type, and aren’t limited to literals. If the left side is greater than the right side, then the increment will be inverted.

var start = 9
var end = 5

for i in start..end {
	c.printf("%d\n", i)  // prints 9, 8, 7, 6
}

We can also alter the increment of a range using the by keyword.

for i in 0..10 by 2 {
	c.printf("%d\n", i)  // prints 0, 2, 4, 6, 8
}

Ranges as arrays

It is possible to cast a range to a dynamically sized array or a fixed array if the size is fully known.

var start = 0
var end   = 200
var list  = []i32(start..end)  // list is now [0, 1, 2, ..., 199]
var fixed = [5]i32(0..4) // fixed is now [0, 1, 2, 3, 4]