lua-users home
lua-l archive

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

Edgar Toernig wrote:
Powerful it may be, but with the complexity I dare to disagree ;-)
It's even more obscure than the previous global statement *g*

I don't agree that it's obscure. Anyway, now that Lua has banished tag methods, inspired from your Sol work, we have room for a little new complexity :-). Especially when it's useful.

  1) You no longer have one "global" table but a lot of them.  The
     term "global" becomes a little bit strange.

It's not so bad. Instead of thinking of "global" to mean the outermost scope or a specific table, I think of it to mean the way I access the variable. In other words, global accesses are those to undeclared variables, and I may want to map such accesses to a certain table. If you insist that "global" must mean the very outer scope, why not also say that "local" should mean only the innermost scope?

  2) The global-table is attached to Lua functions.  You are no longer
     able to switch to another "global" table.  You have to explicitely
     change each and every function (hard/impossible to do).

I don't quite follow the problem here. It's easy to group functions that you would like to share scope within a function or chunk.

  3) It introduces a hard to track dynamic binding.  It's impossible to
     tell which object is accessed by an identifier.  The binding is
     done dynamically at a later point (even while a function is active).

It looks like a feature to me. Writing a chunk with exported variables as globals and then being able to control binding according to the application is very powerful. The old global statement was too static-- a problem, especially since Lua lack's a macro system.

IMHO, even

  local val
  mod_table = {}
  function val=x end
  function print(val) end

is easier to understand than these global table tricks.

What you're not showing is bar calling foo (in other words, code with countless references to mod_table). You're neglecting that to prevent global name clashes and get the best performance, you have to make mod_table local and return it to the caller.

Anway, nothing prevents you from continuing to write in that style.


http:// i      .   /