some brain-breaking const usage
parent
c87ae5ce70
commit
2be7a1be45
@ -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();
|
||||
}
|
@ -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});
|
||||
}
|
@ -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);
|
||||
}
|
@ -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;
|
||||
}
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue