lua-users home
lua-l archive

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


On Aug 5, 2005, at 21:32:06, Roberto Ierusalimschy wrote:
I did not say there was automatic globals. What I meant was this: When
you see a function like

  int foo () { a = 1; }

in C, C++, C#, Pascal, Objective Pascal, Ada, Java, Scheme, Lisp (and a lot of other languages), do you think that "a" is local to the function? Or does it look more like a global (that is, external to the function)?

Roberto,

being a C programmer, in the above function I'd expect it to be global, because a local variable would be declared somewhere. But that's because I have grown used to the way C does things. If this was C++ or TADS, I'd only expect it to be declared in an outside scope, i.e. as a global or an instance variable. If this was PHP, I'd expect this to be local (And pretty pointless at that).

As a human, I'd expect a to be in some outside scope *in this particular example* simply because it would be pretty pointless otherwise to put 1 into a variable (*and* not return anything) and then forget about it. But the Lua compiler can't think like a human, because a function that caches a value could look very different yet still require a global:

int cachedFoo()
{
    if( foo == nil )
        calculateFoo();
    return foo;
}

There is no right answer to this. It's simply a matter of who you expect to be the users of your app and the scripting language embedded in it.

Cheers,
-- M. Uli Kusterer
http://www.zathras.de