A generic function accepts a value when the operations in its body work with that value’s type, rather than requiring an explicitly declared interface. In Zig, a comptime type parameter enables this compile-time duck typing; functions can also check types manually. section 4.1.3
Listing 4.3 Compile-time duck typing
const std = @import("std");
fn sum(comptime T: type, values: []const T) T {
var total: T = 0;
for (values) |value| {
total += value;
}
return total;
}
pub fn main() void {
const result = sum(i32, &.{1, 2, 3});
std.debug.print("sum: {}\n", .{result});
}
The sum function works with i32 values because they support addition; using bool values illustrates an operation the function cannot perform. section 4.1.3



