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>
3.5 KiB
3.5 KiB
9. Generics and interfaces
Generic functions
Put the type names in [ ] after the function name. The compiler works the real types out
from the arguments.
func first[T](xs: []T): T {
return xs[0]
}
func biggest[T: Ordered](a, b: T): T {
if a > b { return a }
return b
}
func main() {
print(first([7, 8, 9]), first(["x", "y"]))
print(biggest(3, 9), biggest("pear", "apple"), biggest(1.5, 0.5))
}
7 x
9 pear 1.5
After the colon you can name a rule for the type:
| Rule | Meaning |
|---|---|
Ordered |
can be compared with < > (numbers and strings) |
Number |
numbers only |
| an interface | has certain methods (see below) |
| (none) | anything |
Generics cost nothing at run time: every set of types gets its own compiled copy.
A generic function can take functions too, which makes map and filter easy to write:
func map[T, U](xs: []T, f: func(T): U): []U {
var out: []U
for x in xs { out.push(f(x)) }
return out
}
func main() {
print(map([1, 2, 3], x => x * 10))
print(map(["a", "bb"], s => s.len()))
}
[10, 20, 30]
[1, 2]
Generic types
Structs and enums can take types too. When you use one, write the types out.
struct Stack[T] {
items: []T
func push_item(x: T) { items.push(x) }
func pop_item(): T { return items.pop() }
func size(): int { return items.len() }
}
enum Maybe[T] {
Some(value: T)
None
}
func main() {
var s = Stack[int]{}
s.push_item(1)
s.push_item(2)
print(s.pop_item(), s.size())
var m = Maybe[string].Some("hi")
print(m)
}
2 1
Maybe[string].Some(hi)
Interfaces
An interface lists methods. A type follows it just by having those methods, with no
implements keyword.
interface Shape {
area(): float
name(): string
}
struct Square {
side: float
func area(): float { return side * side }
func name(): string { return "square" }
}
struct Circle {
r: float
func area(): float { return 3.0 * r * r }
func name(): string { return "circle" }
}
As a rule for generics
interface Shape {
area(): float
}
struct Square {
side: float
func area(): float { return side * side }
}
func report[T: Shape](s: T) {
print("area is", s.area())
}
func main() {
report(Square{3.0})
}
area is 9.0
As a type (dynamic dispatch)
An interface is also a type of its own. A variable of that type can hold any value that has the methods, and the right method runs at run time. This is how you build a mixed list.
interface Shape {
area(): float
name(): string
}
struct Square {
side: float
func area(): float { return side * side }
func name(): string { return "square" }
}
struct Circle {
r: float
func area(): float { return 3.0 * r * r }
func name(): string { return "circle" }
}
func main() {
var shapes: []Shape = [Square{2.0}, Circle{1.0}]
for s in shapes {
print(s.name(), s.area())
}
var one: Shape = Circle{2.0}
print(one.area())
}
square 4.0
circle 3.0
12.0
Details:
- A struct or enum value converts to the interface automatically. The value is copied in.
nilis the empty interface value; calling a method on it stops the program.- Not yet: turning an interface value back into its original type, and interfaces that take their own type parameters.