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:
		| @@ -29,23 +29,32 @@ end | ||||
| -- if tt.init is defined, generated | ||||
| -- new calls tt.init(ret) after | ||||
| -- ret is definitely not nil, | ||||
| -- before calling setmetatable. | ||||
| -- after calling setmetatable. | ||||
| -- use to initialize mutables. | ||||
| -- | ||||
| -- if there was a previous new, | ||||
| -- it is invoked on the new | ||||
| -- object *after* more, because | ||||
| -- this works better with the | ||||
| -- `more` impls i use. | ||||
| -- it is invoked before | ||||
| -- setting tt's metatable, so | ||||
| -- each new will see its | ||||
| -- inheritance chain. | ||||
| 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) | ||||
|   if(not ret) ret = {} | ||||
|   if(more) more(ret) | ||||
|   if(oldnew) oldnew(ret) | ||||
|   setmetatable(ret, mt) | ||||
|   if(oldinit) oldinit(ret) | ||||
|   if (more) more(ret) | ||||
|   return ret | ||||
|  end | ||||
|   | ||||
|  if oldinit and more then | ||||
|   tt.superinit = function(ret) | ||||
|    oldinit(ret) | ||||
|    more(ret) | ||||
|   end | ||||
|  elseif more then | ||||
|   tt.superinit = more | ||||
|  end | ||||
|  return tt | ||||
| end | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user