lua-users home
lua-l archive

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


On mar, 2012-04-10 at 18:33 +0400, Rebel Neurofog wrote:
> By putting "?" into M or by implementing get function to access upvalue(s).
> Sometimes implementing set/get pair for every piece of data is worth it anyway
> (e. g., in widget system), sometimes it's just an overhead.
> 
> Several upvalues may be packed into single table like:
>    M.private = {a = b, c = d, e = f}
> or accessed by single function:
>    function M.get_stuff ()
>        return b, d, f
>    end

But in this case the private bits become visible and available trough
the module table for the module's users, no? Can this be avoided? 
The best idea I thought of is placing all private bits in another table,
and returning it as a second return, like this:

---------------------------------
local M, M_private={}
local aux = function(n) 
        return "I say "..n
end
M_private.aux = aux
M.exclamate = function(s) 
        return aux(s).."!"
end
return M, M_private
---------------------------------

Normal users would do  

local m=require "m"

And thus only se the public interface. A submodule would do

---------------------------------
local m, private=require "m"
m.wonder = function(s) 
        return private.aux(s).."?"
end
---------------------------------

This doesn't stop a final user from getting the second return and
fiddling with it, but at least doesn't confuse the unsuspecting with
"garbage" in the module table.


Jorge