lua-users home
lua-l archive

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


Hi,

Miles Bader wrote:
For a while, I've tended to use require like:

   local XX = require 'whatever'
   XX.whatever()

This seems to work with almost every module I've tried it with, and
offers a nice sense of locality.

[The alternative, of course, is to assume the loaded package will
put a "whatever" entry in _G.]

However I've just realized that some things _don't_ return an
appropriate table from require.  In particular swig-generated interfaces
seem to simply return the string "whatever"!  I could swear this _used_
to work with swig-generated interface, but it's been some months since I
last did something that would test that.

So, what exactly _is_ the recommended way to do this?  Is swig being bad
by returning a string instead?  Should I change my style of using
modules?


Can't comment on swig, but I'm using your style. In addition, I'm writing my lua code as modules too (learned this style from Petite Abeille's code):

--yyyy.lua
--imports
local xx = require"xxxx"
local setmetatable = setmetatable
local require = require
local print = print
module("yyyy")

local function fa() print"yyyy.fa" end
local function fb() print"yyyy.fb" end

--simulate ctor
function __call(self, ...)
  local t = {}
  t.a = fa
  -- not public! t.b = fb
  return t
end
setmetatable(_M, _M)

--zzzz.lua
local Y = require"yyyy"
local y = Y()
y:a()
---

Thanks,

-Miles



--
Regards,
Hakki Dogusan