2 ms·
What Is Zig's Comptime?
- komuW 7y agoquite 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; }