lua-users home
lua-l archive

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



> (BTW, why do you need to return `module', if it came from the outside?)

Convenience, mostly. It makes the code a little shorter. My feeling is that
OO-like methods should always return self if they can't think up anything
better to return, because then you can reuse the value which you often want
to do.

Question of style, I suppose. But it reduces the need to think up names for
local variables you are only going to use once.

On this subject: setmetatable(tab, meta) returns tab
But: setfenv(func, env) does not return func.

It could be argued that setfenv() does not necessarily take
a function as its first argument (although I personally think the
numeric argument to setfenv should be deprecated), but it still
would be nice to be able to say, eg.

local flag, foo = pcall(setfenv(assert(loadfile(script)), myEnv), bar)

instead of

local flag, foo
do
  local chunk = assert(loadstring(input))
  setfenv(chunk, myEnv)
  flag, foo = pcall(chunk, bar)
end

----

Of course, not everyone will like the style of the first line, I guess.
But I do :)

PD: No chance of a "let" statement I don't suppose?

let chunk = assert(loadstring(input)) in
  setfenv(chunk, MyEnv)
  local flag, foo = pcall(chunk, bar)
end

Some people probably wouldn't like the odd scoping of the internal local.