lua-users home
lua-l archive

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

Alexander Gladysh <> wrote:
> Is there something more graceful than straight:
> local f1 = function(t)
>  t.method1 = method1e
>  t.method2 = method2

Just put your methods into a table, then your factory has only one upvalue and you can copy the methods into a new table with a loop.

function clone(t, rv) -- shallow table copy
   rv = rv or {}
   for k,v in pairs(t) do rv[k] = v end

local factory
   local prototype = {}
   function prototype:method1() end
   function prototype:method2() end
   function prototype:methodN() end
   factory = function() return clone(prototype) end