14. Generics: comptime
A comptime T: type parameter takes a type, known at compile time. A
function with one is instantiated for each set of type arguments, and each
instance is type-checked on its own, as in Zig:
fn max(comptime T: type, a: T, b: T) T { ... } // max(i64, 3, 9)
A type function returns type and its body is exactly
return struct { ... }. Pair(i64) then names a struct type; inside the
struct, @This() is that type.
A comptime argument must be a type (E125), and type values exist only at
compile time: no variable or field can hold one.
// expect: 2
// expect: 9
// expect: Pair(bool)
uses io
fn Pair(comptime T: type) type {
return struct {
a: T
b: T
fn swap(self: @This()) @This() {
return @This(){ .a = self.b, .b = self.a }
}
}
}
fn max(comptime T: type, a: T, b: T) T {
if (a > b) {
return a
}
return b
}
fn main(io: Io) void {
const p = Pair(i64){ .a = 1, .b = 2 }
io.print(p.swap().a)
io.print(max(i64, 3, 9))
io.print(@typeName(Pair(bool)))
}
Choosing by type. An if whose condition compares types (K == str,
K != i64, joined by and, or and !) is decided at compile time:
only the branch it chooses is checked and generated, so one generic
function can do different things per type, and an else if chain picks
the first branch whose types match. Types compare nowhere else (E103).
@compileError("message") reports E146 with that message where it is
checked, so it marks a branch no type should choose; such a branch needs
no return.
// expect: 3
// expect: 7
uses io
fn size(comptime T: type, x: T) i64 {
if (T == str) {
return @len(x)
} else if (T == i64) {
return x
} else {
@compileError("size takes a str or an i64")
}
}
fn main(io: Io) void {
io.print(size(str, "abc"))
io.print(size(i64, 7))
}
Choosing by what a type declares. @hasDecl(T, "name") is true when
T has a method of that name and @hasField(T, "name") when it has a field
of that name (a union’s member, an enum’s value). They are conditions of
the same kind and join type comparisons with and, or and !, so code
calls a method only for the types that have it. Only types the program
declares, itself or through a type function, have methods and fields
here: for i64, str, Buf(T) and the other built-in types both are
false. The name is a string literal, and both are used nowhere else (E103).
// expect: 12
// expect: 5
// expect: 7
// expect: -1
uses io
const Point = struct {
x: i64
y: i64
fn code(self: Point) i64 {
return self.x * 10 + self.y
}
}
const Size = struct {
x: i64
}
fn code(comptime T: type, v: T) i64 {
if (@hasDecl(T, "code")) {
return v.code()
} else if (@hasField(T, "x")) {
return v.x
} else if (T == i64) {
return v
} else {
return -1
}
}
fn main(io: Io) void {
io.print(code(Point, Point{ .x = 1, .y = 2 }))
io.print(code(Size, Size{ .x = 5 }))
io.print(code(i64, 7))
io.print(code(str, "abc"))
}
// error: E146
uses io
fn size(comptime T: type, x: T) i64 {
if (T == str) {
return @len(x)
} else {
@compileError("size takes a str")
}
}
fn main(io: Io) void {
io.print(size(bool, true))
}