#Naming conventions - When to use init() and when create()

1 messages · Page 1 of 1 (latest)

raven matrix
#

From what I've seen, init() is more common for functions that don't allocate, i.e. return a plain struct, and create() for ones that do allocate and return a pointer.

bright lichen
#

There's no real hard rule about it, name your methods whatever makes the most sense. Another common convention is for a type not to have an init function, but instead to exist alongside a function of the same name, which can be nice for allowing more type inference, e.g., take std.io.fixedBufferStream, which returns a std.io.FixedBufferStream(T). Or sometimes types just don't have initialisation functions, because the process to initialise them is trivial, e.g. like std.Thread.Mutex, or std.ArrayListUnmanaged(T).