#Why do the `std.mem.Allocator` methods pass self by value?

1 messages · Page 1 of 1 (latest)

quaint nebula
#

I'm trying to understand the design decision behind it. It seems like it should be passed by const reference. Does this have some good optimization property of which I'm unaware?

cursive linden
#

It's a vtable

#

has only pointers

#

already constant

#

and if zig can decide by itself if you need to pass it by reference

#

(Arguments in zig are inmutable)

quaint nebula
cursive linden
#

yes

#

By the langref:

Structs, unions, and arrays can sometimes be more efficiently passed as a reference, since a copy could be arbitrarily expensive depending on the size. When these types are passed as parameters, Zig may choose to copy and pass by value, or pass by reference, whichever way Zig decides will be faster. This is made possible, in part, by the fact that parameters are immutable.

quaint nebula
#

Ok, that's really what I wanted to know. I have a struct which is very similar. It just contains a std.io.Reader and an optional std.mem.Allocator. I realized I could change all my *Struct into *const, then I realized I could effectively do it by value.

#

Thank you for your help.