Files
rexlang/examples/basics/list_values.dino
T
Your NameandClaude Sonnet 5.5 eaf9567a36 Dino compiler: language, formatter, language server, docs, editor support
Compiler in Rust (lexer, parser, checker, C codegen), maps, chars,
dino fmt, dino lsp (diagnostics, formatting, outline, definition,
hover, completion, signature help), docs, examples, VS Code/Vim files.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 23:46:12 +01:00

69 lines
1.2 KiB
Plaintext

// Lists are values: assigning or passing copies them.
struct Inventory {
items: []string
gold: int
func add(item: string) {
items.push(item)
}
func count(): int {
return items.len()
}
}
func add_one(xs: []int): []int {
xs.push(1)
return xs
}
func first(xs: []int): int {
return xs[0]
}
func main() {
var a = [1, 2, 3]
var b = a
b[0] = 99
b.push(4)
print(a, b)
// a function that changes its parameter works on its own copy
var c = add_one(a)
print(a, c)
print(first(a), first(c))
// structs holding lists copy deeply
var inv = Inventory{[], 10}
inv.add("sword")
var saved = inv
inv.add("shield")
saved.gold = 0
print(inv)
print(saved)
print(inv.count(), saved.count())
// nested lists
var grid = [[1, 2], [3, 4]]
var snapshot = grid
grid[0][1] = 20
grid[1].push(5)
print(grid)
print(snapshot)
// looping over a list while changing it walks the original
var xs = [1, 2, 3]
for x in xs {
xs.push(x * 10)
}
print(xs)
// the loop variable is a copy too
var ys = [1, 2, 3]
for y in ys {
y += 100
print(y)
}
print(ys)
}