Lua Notes

Zero Is Initialization
Login

Zero is Initialization

The convention that zeroing a block of memory will result in an immediately useful, initialized type.

c.f. zii.mp4, an excerpt from Handmade Hero Day 341 - Dynamically Growing Arenas

nelua examples

local function f1(n: integer)
  print(n)
end
f1() -- zero is passed

require 'hashmap'
local map: hashmap(integer, string)
map[100] = "one hundred"
print(map[100]) -- one hundred

edubart remarks

from discord.

no, Nelua has no constructors and won't have, they introduce many unwanted consequences and complexities in the language, you can workaround that by creating a explicit constructor, or use ZII more (zero is initialization)

Nelua libraries usually takes advatange of ZII, for example the vector library, you can use push method in any vector that is initialized with zeros, and Nelua initializes everything with zeros by default, as a consequence you can use push in any vector object, it will automatically initialize needed stuff

in ZII, you can also think of designing your data structures where zero initialized objects have meaning, where usually an object where everything are set to zeros is an "empty" object

and as soon any object method is called for an "empty" object, some of its fields are then updated