lua-users home
lua-l archive

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

On Jul 22, 2009, at 12:19 PM, startx wrote:

not that i want to discuss taste only ;) ... but what would be your
alternative suggestion to organise a huge bunch of functions? im open
to try different flavours ...

There are many ways to skin that cat, but I personally use module( 'MyModule' ) and that's all.

In general, one file, one named module, one private name space, one public API exposed through meta methods.

For example, assuming a module 'HMAC':

% cat HMAC.lua

-- import external dependencies
local getmetatable = getmetatable
local setmetatable = setmetatable

-- explicitly name the module
module( 'HMAC' )

-- add meta table when appropriate
local self = setmetatable( _M, {} )
local meta = getmetatable( self )

-- do stuff in the privacy of the module
local function HMAC( aKey, aValue )

-- expose stuff to the outside
function meta:__call( aKey, aValue )
    return HMAC( aKey, aValue )

FWIW, the above convention is followed by Nanoki:

As with many other things in Lua, it takes a while to come up with an organizational style which consistently support one programming mannerism due to Lua's inherent flexibility.