#Naming conventions: init, deinit, create, destroy, etc

1 messages · Page 1 of 1 (latest)

glass swallow
#

Is there a guide or blog post or something describing some of the naming conventions in Zig with respect to constructor/initializer/factory functions and their corresponding deconstructors/deinitializers/free/etc functions?

#

seems like: init and deinit generall operate on non-pointer values; create and destroy generally operate on pointers and allocate/free memory.

is that right? are there other related rules & conventions?

vast vortex
#

that about sums it up. You also often see initWith<Something>, where <Something> is something like Allocator, Capacity - that sort of thing

#

Or sometimes a type doesn't need a constructor, because it can be default-initialised (like std.ArrayListUnmanaged can be f.e.)

#

that's also a convention often seen in the stdlib, and sometimes userland code/libraries: <T>Managed vs <T>Unmanaged

#

it usually refers to whether or not a data structure carries around its allocator or not

glass swallow
#

gotcha. thanks. seems like the "default" convention is "unmanaged"? ie, for both create & destroy accept a matching allocator as params?

#

which is what i'd expect to avoid the extra pointer(s) stored everywhere

vast vortex
#

Well, it depends

#

the rule of thumb is: if you're storying it as a variable, Managed is fine, because it's probably going to be used often

#

if it's stored in-memory (e.g. an arraylist of arraylist), then you're better of using an unmanaged version, so you can share a single allocator value among many unmanaged data structures in-memory

#

(and also to save space on the 16 extra bytes per item)