PDA

View Full Version : Ew ew ew ew ew (horrible Lua design flaw.)



Ian
02-24-2007, 06:43 PM
Lua always creates a new function object for each inner function whenever the outer function is invoked instead of optimizing it away when possible. (Okay, this isn't really a big deal now that I think about it.)

Because my class() thingy used a closure I made a version that doesn't. It's probably more readable anyway.


do
local function call(cls, ...)
instance = setmetatable({}, cls)
if cls.__init then
cls.__init(instance, ...)
end
return instance
end

function class()
local cls = {}
cls.__index = cls
return setmetatable(cls, {__call = call})
end
end

Note: because of bizarre scoping rules "call" has to precede "class". Do you get a decent error message if you do otherwise? Yeah, right.