zig/doc/langref/test_comptime_variables.zig

21 lines
466 B
Zig

const std = @import("std");
const expectEqual = std.testing.expectEqual;
test "comptime vars" {
var x: i32 = 1;
comptime var y: i32 = 1;
x += 1;
y += 1;
try expectEqual(2, x);
try expectEqual(2, y);
if (y != 2) {
// This compile error never triggers because y is a comptime variable,
// and so `y != 2` is a comptime value, and this if is statically evaluated.
@compileError("wrong y value");
}
}
// test