zigo
1
For example, is the following program guaranteed to print 0 1 1
.
In Go, there are not such guarantees.
const std = @import("std");
const T = struct {
x: u32,
fn inc(t: *@This()) u32 {
t.x += 1;
return t.x;
}
};
pub fn main() void {
var t = T{.x = 0};
std.debug.print("{} {} {}\n", .{t.x, t.inc(), t.x});
// 0 1 1
}
kj4tmp
2
It’s not codified in the language reference, but yes as far as I can tell.
Related: Do struct field initializations occur in well defined order?
1 Like
zigo
3
It is great if this is guaranteed.
ajoino
4
I’m scared to ask, but what does Go do that means it cannot guarantee the order?