lua-users home
lua-l archive

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


Why not just have something like an interpreter switch 

 -defaultvarscope=global|local

Old scripts will run as expected, and new scripts can make use
of better data hiding.


-----Original Message-----
From: Tom _ [mailto:tom7ca@yahoo.com]
Sent: Thursday, May 24, 2001 10:26 AM
To: Multiple recipients of list
Subject: proposal (RE: Making vars local by default)


I have thought about different ways of 
achieving this that doesn't break backwards
compatibility.

I think the best solution is to introduce
an additional assignment operator with the
desired semantics (say, ":=").  That requires
no changes to existing code, and it's easy
to do.

In addition, it might be worthwhile to
introduce a "global" keyword that overrides
the "local" behavior of the new assignment
operator.  Then, new code could be written
entirely using ":=" and "global".  That would
allow new Lua programmers to adopt a familiar
and consistent style.  Optionally, the system could
warn about uses of the old-style assignment if
desired, to help people avoid accidents.

I think people don't generally appreciate how serious
of a problem the current semantics are.  Accidental
use of global variables is a sleeper: it may be
present in your code for a long time and not show up 
until you make completely unrelated changes to your 
code. The consequences of such bugs can be very subtle
and very hard to track down.  As a programmer, that's
really creepy to me.  And there is no simple rule 
people can follow that reliably avoids
these bugs given the currently available language
constructs.  With something like ":=", there would be.

Tom.

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/