Hi all, on my journey to learn zig I was a bit confused with all the discussion around the pass struct by reference or value.
My main point of confusion came from reading this discussion: Pass by value semantics.
While learning the language, I tried to follow as closely as possible all the advices on the 0.16 documentation, but from the discussion above it seems that it is not quite updated.
When is it advisable passing a struct via value or reference? Coming from C++, I was prepared to pass everything by reference (apart from basic types), but looking at the documentation and some other threads, it looked like the “Zig Way” was to just default to pass by value and let the compiler decide what to do.
For example if I have a struct that holds the allocator and IO interfaces, should I pass it by value or reference? I ask because each of these interfaces will hold a bunch of pointers, of non-negligible size.
const struct Context = struct {
io: std.Io,
allocator: std.mem.Allocator,
};
fn foo(ctx: Context) {...}
// or
fn foo(ctx: *const Context) {...}
As I understand this changed recently, so what is the current advisable way?
Thanks!