On 28/04/2009, at 1:09 PM, David Manura wrote:
On Mon, Apr 27, 2009 at 4:15 PM, Geoff Leyland wrote:
The default package.path contains './?.lua', but not './?/
init.lua'.
Obviously, it's easy to add, so there's no problem, but I often
find
myself
adding it. Any reason it's not there by default?
Perhaps we should instead ask why init.lua it not eliminated
entirely.
It is not necessary[1], and I think it complicates things[2].
Module
authors need to decide which form to use, and it's near certain that
different module authors will choose different conventions. Module
users or Lua distributions, when adding a directory to the Lua
search
path, may need to add both forms in general, though omitting the
latter might usually work or might appear to initially work. It
also
opens the ambiguity where ".../a/init.lua" could represent the
module
"a" or a module actually named "a.init". Finally, it makes module
search errors longer for the end-user:
While we're discussing packages, what's the logic behind the
difference
between what's returned by require and what's put in _G?
mod.lua:
module(...)
a = 1
return 3
print(require("mod"))
2
print(type(mod))
table
This may seem irrelevant, but I often put a single class or
function into a
file:
obj.lua:
local setmetable = setmetatable
module(...)
obj = {}
function obj:new()
self.__index = self
return setmetatable({}, self)
end
and create a new obj I have to go:
require("obj")
o = obj.obj:new()
which is a bit awkward.
An alternative is to put a "return obj" at the end of obj.lua, and
then I
need to go
local obj = require("obj")
o = obj:new()
Where I have to add the "local obj ="
One workaround is:
obj.lua:
local setmetable = setmetatable
module(...)
function new(self)
self.__index = self
return setmetatable({}, self)
end
require("obj")
o = obj:new()
But I kind of get the feeling I'm not Doing The Right Thing in all
these
cases. I don't have any solution to this, or any strong opinions,
just
wondering if anyone has a better idea.
Cheers,
Geoff