lua-users home
lua-l archive

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


Gunnar Zötl:
> So if I understood it correctly, not only can
> every function have its own "global" table, but it is indeed the case
> that each function has a reference to a global table, which, in the
> default case, is the top level global table.

Yes... in a variable named "_G".

So:
  xyzzy = 123
and
  _G.xyzzy = 123

are the same thing.


> So, basically, setglobals() just looks like a weird way to introduce a
> dynamic binding mechanism. But, unlike perls 'local' or phps 'global', you
> have to explicitely tell each and every function which global table to
> use, should you want to use the dynamic binding.

It now seems that way to me, after examining your example. I guess it's
something I haven't though much about but originally I would have made the
same assumption you did.


> From the name, and the description, I somehow assumed that it would really
> alter the "global" table for the time the setglobals()d function is
> active. So, you're right, it is consistent, in a way, but somehow
> counterintuitive. If this is the intended behaviour of this function, I
> feel that the name should be changed, and there should be in the finished
> docs explaining this...

Absolutely! I agree thoroughly about the docs & name change.


> btw. I have been told that in lua4, setglobals() did indeed change the
> globals table in the way I assumed it would do.

It did. Now you access the global table _G directly! So Lua4
  old = setglobals(sb) ; blah ; setglobals(old)

becomes in Lua5
  old,_G=_G,sb ; blah ; _G=old

The "setglobals" name has now been given a totally new meaning! Perhaps
"setlocal" might be a better name as it seems to "localise" global
references in the specified function's lexical scope.

*cheers*
Peter Hill.