3 ms·
quite clever and pretty powerful, you can even use it to come up with generic constraints/concepts/contracts in userland without language support. Something alo
by komuW 7y ago
quite clever and pretty powerful, you can even use it to come up with generic constraints/concepts/contracts in userland without language support. Something along the lines of:
// A generic Max function that only works for Float & Ints
fn Max(comptime T: type, a: T, b: T) T {
switch (@typeInfo(T)) {
.Float => {}, // all floats are ok
.Int => {}, // all ints are ok
else => {
@compileError("Max not allowed for type: " ++ @typeName(T));
},
}
return if (a > b) a else b;
}