lua-users home
lua-l archive

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


On Thu, Aug 21, 2008 at 1:25 PM, Alexander Gladysh <agladysh@gmail.com> wrote:
> 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).

put all methods in a separate file, prepend them with a module().
when you require() that file, you'll get a table with all non-local
functions.

what module() does is mainly creating a new table, and setting it as
the environment.  after that, all non-local functions will populate
that table instead of the original _G.

i've done this when i want a table with functions and don't want to
write something like:

t = {
 f1=function(...) ... end,
 f2=function(..)  ... end,
.....
}

with module() it's just

module()

function f1 (...)
  .....
end

function f2 (...)
 .....
end


i really want to like the first option, since i know it's the most
'functional', but the second one is a lot nicer to the eye, and using
module(), any non-local value gets stuffed into my table, so it works
great.

-- 
Javier