test_comptime_variables.zig (450B) - Raw
1 const std = @import("std"); 2 const expect = std.testing.expect; 3 4 test "comptime vars" { 5 var x: i32 = 1; 6 comptime var y: i32 = 1; 7 8 x += 1; 9 y += 1; 10 11 try expect(x == 2); 12 try expect(y == 2); 13 14 if (y != 2) { 15 // This compile error never triggers because y is a comptime variable, 16 // and so `y != 2` is a comptime value, and this if is statically evaluated. 17 @compileError("wrong y value"); 18 } 19 } 20 21 // test