lua-users home
lua-l archive

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


Hi, list!

When defining an object, I follow this idiom:

local factory
do
  local method1 = function(self)
    print("method1", tostring(self))
  end

  local method2 = function(self)
    print("method2", tostring(self))
  end

  local methodN = function(self)
    print("methodN", tostring(self))
  end

  factory = function()
    return
    {
      method1 = method1;
      method2 = method2;
      --...
      methodN = methodN;
    }
  end
end

One of my most ugly objects now have more than 60 methods (yes, I
know, I should refactor this abomination out immediately, but I just
do not have time for it yet). I guess I have to split my factory
function to smaller ones, populating object table with smaller batches
of methods. Is there something more graceful than straight:

local f1 = function(t)
  t.method1 = method1
  t.method2 = method2
  -- ...
end

local fN = function(t)
  -- ...
  t.methodN = methodN
end

factory = function()
  local t = {}
  f1(t)
  -- ...
  fN(t)
  return t
end

Thanks in advance,
Alexander.