So a recent post I’ve come across made me perform the following test:
function constructor()
local self = {}
function self.blah()
end
return self
end
local cache = {}
for i = 1, 10 do
table.insert(cache, constructor())
print(cache[i].blah)
end
Interestingly enough, despite defining the function as a member of each individual table that gets allocated, it would seem to print the same memory address (assuming that’s what is actually printing, it would appear to be that way)
I’ve tried to find Lua documentation on this subject, but I can find nothing. Based on my tests, it would appear that function definitions are allocated at compile time, and then references to those predefined functions are used as opposed to dynamically allocating memory for a new function entirely.
Really not sure though. Would appreciate any clarification about this, thanks.