rewrite mknew to inherit fields before init
I am horrified to admit that C++'s constructor static initialization list syntax abruptly makes sense to me and I understand the problem it is trying to solve
This commit is contained in:
parent
abee6d1206
commit
9b3120c47b
@ -29,23 +29,32 @@ end
|
|||||||
-- if tt.init is defined, generated
|
-- if tt.init is defined, generated
|
||||||
-- new calls tt.init(ret) after
|
-- new calls tt.init(ret) after
|
||||||
-- ret is definitely not nil,
|
-- ret is definitely not nil,
|
||||||
-- before calling setmetatable.
|
-- after calling setmetatable.
|
||||||
-- use to initialize mutables.
|
-- use to initialize mutables.
|
||||||
--
|
--
|
||||||
-- if there was a previous new,
|
-- if there was a previous new,
|
||||||
-- it is invoked on the new
|
-- it is invoked before
|
||||||
-- object *after* more, because
|
-- setting tt's metatable, so
|
||||||
-- this works better with the
|
-- each new will see its
|
||||||
-- `more` impls i use.
|
-- inheritance chain.
|
||||||
function mknew(tt)
|
function mknew(tt)
|
||||||
local mt,oldnew,more = {__index=tt},tt.new,rawget(tt, "init")
|
local mt,oldinit,more = {__index=tt},tt.superinit,rawget(tt, "init")
|
||||||
tt.new=function(ret)
|
tt.new=function(ret)
|
||||||
if(not ret) ret = {}
|
if(not ret) ret = {}
|
||||||
if(more) more(ret)
|
|
||||||
if(oldnew) oldnew(ret)
|
|
||||||
setmetatable(ret, mt)
|
setmetatable(ret, mt)
|
||||||
|
if(oldinit) oldinit(ret)
|
||||||
|
if (more) more(ret)
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if oldinit and more then
|
||||||
|
tt.superinit = function(ret)
|
||||||
|
oldinit(ret)
|
||||||
|
more(ret)
|
||||||
|
end
|
||||||
|
elseif more then
|
||||||
|
tt.superinit = more
|
||||||
|
end
|
||||||
return tt
|
return tt
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user