const std = @import("std");
fn foo(newval: ?u32) u32 {
const static = struct {
var foo: u32 = 1337;
};
if(newval) |v| static.foo = v;
return static.foo;
}
test {
try std.testing.expectEqual(@as(u32, 1337), foo(null));
try std.testing.expectEqual(@as(u32, 69), foo(69));
try std.testing.expectEqual(@as(u32, 69), foo(null));
}
#Is this code guaranteed to behave as expected?
1 messages · Page 1 of 1 (latest)
sure, so long as you're always single threaded
yes, it is exactly the same as
var foo_val: u32 = 1337;
fn foo(newval: ?u32) u32 {
if(newval) |v| foo_val = v;
return foo_val;
}
oh no I mean in this specific case
yeah
good
foo is just a normal global variable
I was worried that the semantics of initializing a global in a function weren't defined
nah, they is
good ^_^