diff --git a/comptime_vars.zig b/comptime_vars.zig new file mode 100644 index 0000000..e69de29 diff --git a/const.zig b/const.zig new file mode 100644 index 0000000..f4479d9 --- /dev/null +++ b/const.zig @@ -0,0 +1,13 @@ +const x = 1234; + +fn foo() void { + // can const inside function scope + const y = 5678; + + // this is prevented presumably at compile time + y += 1; +} + +test "assignment" { + foo(); +} diff --git a/const2.zig b/const2.zig new file mode 100644 index 0000000..dae8a9a --- /dev/null +++ b/const2.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const print = std.debug.print; + +const x: i32 = 1234; + +pub fn main() void { + print("x: {}\n", .{x}); + // confirmed, this program can't be built, so this guarantee is checked at + // compile-time, which is what I expected. + x += 1; + print("x: {}\n", .{x}); +} diff --git a/initialization.zig b/initialization.zig new file mode 100644 index 0000000..7423afa --- /dev/null +++ b/initialization.zig @@ -0,0 +1,8 @@ +const expect = @import("std").testing.expect; + +test "initialization" { + // weird but ok. undefined and null both existing! I hate it. + var x: i32 = undefined; + x = 1; + expect(x == 1); +} diff --git a/namespaced_global.zig b/namespaced_global.zig new file mode 100644 index 0000000..c725577 --- /dev/null +++ b/namespaced_global.zig @@ -0,0 +1,21 @@ +const std = @import("std"); +const expect = std.testing.expect; + +test "namespaced global variable" { + expect(foo() == 1235); + expect(foo() == 1236); + expect(boffin() == 1237); +} + +fn foo() i32 { + const S = struct { + var x: i32 = 1234; + }; + S.x += 1; + return S.x; +} + +fn boffin() i32 { + S.x += 1; + return S.x; +} diff --git a/threadlocal.zig b/threadlocal.zig new file mode 100644 index 0000000..5070361 --- /dev/null +++ b/threadlocal.zig @@ -0,0 +1,18 @@ +const std = @import("std"); +const assert = std.debug.assert; + +threadlocal var x: i32 = 1234; + +test "thread local storage" { + const thread1 = try std.Thread.spawn({}, testTls); + const thread2 = try std.Thread.spawn({}, testTls); + testTls({}); + thread1.wait(); + thread2.wait(); +} + +fn testTls(context: void) void { + assert(x == 1234); + x += 1; + assert(x == 1235); +}