Closed
Description
Came across this behavior, looks like a bug to me:
const std = @import("std");
const Nested = struct {
x: u32,
};
const Test = struct {
nested: Nested,
fn update(self: *Test, arg: Nested) void {
std.debug.print("t={} arg={}\n", .{self, arg});
self.nested = .{ .x = 0 };
std.debug.print("t={} arg={}\n", .{self, arg});
self.nested = arg;
std.debug.print("t={} arg={}\n", .{self, arg});
}
};
pub
fn main() void {
var t = Test{ .nested = .{ .x = 42 }};
t.update(t.nested);
}
will print:
t=Test{ .nested = Nested{ .x = 42 } } arg=Nested{ .x = 42 }
t=Test{ .nested = Nested{ .x = 0 } } arg=Nested{ .x = 0 }
t=Test{ .nested = Nested{ .x = 0 } } arg=Nested{ .x = 0 }
When passing both a pointer to a struct and a member of this struct, the member is passed by reference (it seems), so changing the struct will change the second argument.
Tested with zig-linux-x86_64-0.9.0-dev.946+6237dc0a.
Metadata
Metadata
Assignees
Labels
No labels