Array.get
func Array.get[T](xs: Array[T], i: int): T | nullArray.get returns element i of xs, or null when i is out of range. Unlike xs[i], it never panics.
Details
- Returns
nullwheniis out of range, a negativeiincluded.xs[i]panics instead. - When
Tincludesnull, anullresult does not tell a missing element from a storednull; compareiwithlen(xs).
Basic examples
Array.get([10, 20, 30], 1) // 20
Array.get([10, 20, 30], 3) // nullCall it through . on any array:
let fruit = ["apple", "pear", "plum"]
fruit.get(0) // "apple"
fruit.get(9) // nullScope
The last valid index is the length minus one:
let xs = [10, 20, 30]
xs.get(xs.length() - 1) // 30
xs.get(xs.length()) // nullA negative index is out of range. Ant does not count from the end:
[10, 20, 30].get(-1) // nullAn empty array has no index in range. Annotate it so that T is known:
let empty: Array[string] = []
empty.get(0) // nullRecords come back as they are stored, and ?. reads a field of the result:
type User = { id: int, name: string }
let users = [User{ id: 1, name: "Ann" }, User{ id: 7, name: "Bob" }]
users.get(1) // User{ id: 7, name: "Bob" }
users.get(1)?.name // "Bob"
users.get(2)?.name // nullChain it with ?. to reach into a nested array:
let grid = [[1, 2], [3, 4, 5]]
grid.get(1)?.get(2) // 5
grid.get(0)?.get(2) // null
grid.get(5)?.get(0) // nullIndex the graphemes of a string by splitting it with String.graphemes first:
"🐜🐝🐞".graphemes().get(1) // "🐝"In a pipeline, the array is the first argument:
"a,b,c" | String.split(",") | Array.get(2) // "c"Applications
Fall back to a default with ??:
let names = ["Ann", "Bob"]
names.get(0) ?? "nobody" // "Ann"
names.get(4) ?? "nobody" // "nobody"Read optional command-line arguments by position:
let args = Os.args()
let input = args.get(0) ?? "data.csv"
let output = args.get(1) ?? "out.txt"
println("${input} -> ${output}")report.csv -> out.txtLook at the neighbors of an element without testing the bounds by hand:
let temps = [12, 15, 11, 18]
for i in Array.range(0, temps.length()) {
let left = temps.get(i - 1) ?? temps[i]
let right = temps.get(i + 1) ?? temps[i]
println("${i}: ${left} ${temps[i]} ${right}")
}0: 12 12 15
1: 12 15 11
2: 15 11 18
3: 11 18 18Return early from a function when the index is out of range, with ?? return:
func weekday(n: int): string {
let name = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].get(n - 1) ?? return "day ${n}?"
"day ${n} is ${name}"
}
println(weekday(3))
println(weekday(8))day 3 is Wed
day 8?Properties and relations
Inside the range, Array.get agrees with xs[i]; outside it, the result is null:
let xs = [4, 9, 16]
for i in Array.range(-2, 5) {
if i >= 0 && i < xs.length() {
assert(xs.get(i) == xs[i])
} else {
assert(xs.get(i) == null)
}
}Index 0 is Array.first, and the length minus one is Array.last, for an empty array too:
let xs = ["x", "y", "z"]
let empty: Array[string] = []
assert(xs.get(0) == xs.first())
assert(xs.get(xs.length() - 1) == xs.last())
assert(empty.get(0) == empty.first())
assert(empty.get(-1) == empty.last())A record it returns is the one in the array, not a copy, so a change to it shows in the array:
type Item = { name: string, stock: int }
let items = [Item{ name: "nut", stock: 0 }]
let item = items.get(0)
if item != null {
item.stock = 10
}
assert(items[0].stock == 10)Possible issues
The result may be null, so using it as a T without narrowing is a compile error:
let xs = [10, 20, 30]
let v: int = xs.get(1) // error: cannot use int | null where int is expected
println(v)Use ?., ??, or a test against null first, as in the examples above.
Testing the call does not narrow a second call, which is still User | null:
type User = { id: int, name: string }
let users = [User{ id: 1, name: "Ann" }]
if users.get(0) != null {
println(users.get(0).name) // error: User | null has no field name: narrow the value first
}Store the result in a let first, then test the variable, as the record example in Properties and relations does.
xs[i] is not the same: it panics where Array.get returns null:
[10, 20, 30][3] // panic: index 3 out of range for an array of length 3When the element type includes null, a stored null and an index out of range look the same. Compare the index with the length to tell them apart:
let scores: Array[int | null] = [90, null]
scores.get(1) // null
scores.get(5) // null
1 < scores.length() // true
5 < scores.length() // falseThe index must be an int. Numeric types never mix, so a float64 is a compile error:
let xs = [10, 20, 30]
println(xs.get(1.0)) // error: cannot use float64 where int is expected?.[i] is not a safe index: it guards against a null array, not an index out of range, and it needs a left side whose type includes null. Use Array.get for the index:
let xs = [10, 20, 30]
println(xs?.[5]) // error: ?.[ needs a nullable left side, got Array[int]See also
Array.first · Array.last · Array.length · Array.indexOf · Array.find · Map.get · ?. operator · ?? operator
