lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Dirk Laurie once stated:
> 
> Here is my full and complete class system. It is written to be the
> body of a file but I often simply insert the code sans comments and
> final 'return' at the top of a program.
> 
> local new = function(class,...)
>   local object = setmetatable({},class)
>   if object.init then object:init(...) end
>   return object
> end
> 
> local class = function(name,index,newindex)
>   local mt = setmetatable({__name=name,__newindex=newindex},
>     {__call=new,__index=index})
>   mt.__index = mt
>   return mt
> end
> 
> local is = function(class,object)
>   return getmetatable(object) == class
> end
> 
> if _ENV.is==nil then _ENV.is = is end
> return class

  Why not make this code its own module?  It seems useful enough.

  -spc