lua-users home
lua-l archive

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

> Now if we add the global keyword, the global("name",
> value) function, or the := assignment 
> (which one is best is a good question)
> all of which do assignment ONLY to globals.

Just to clarify, my proposal was different.  It
was to leave the semantics of "=" unchanged
and to use ":=" for local assignment.  That way,
existing code would continue to work without
any changes and there are no other additions
or mode switches needed for the language.
This would be a complete, self contained
proposal that would address most problems.

For additional safety, one might consider adding
a "global" keyword that could occur inside functions
and that works together with ":=" assignment to
undo the "local" declaration implied by ":=".  Why
would one want to do that?  Because existing code
would still continue to run unchanged, but a
compiler could warn if it detects simultaneous use
of ":=" and "=" in the same function.  Again,
there are no global mode switches.  In fact, there
are no mode switches at all, only two self-consistent
programming styles that can be mixed freely and

For concreteness, "=" style:

function f()
	local x
	x = 3 -- local
	y = 4 -- nonlocal

function g()
	return x -- nonlocal

This is the new style witih ":=":

function f()
	global y
	x := 3 -- local
	y := 4 -- nonlocal

WARNING or ERROR from the compiler:

function f()
	x := 3 -- local
	y = 4 -- ERROR 
	      -- (old style access in new style fun)

Other cases:

function f()
	return x -- works as before

function f()
	-- I would prefer x to be local in this case
        -- because the use of "x :=" declares it local
        -- for the whole function, but it would also
        -- be safe to treat it as non-local until
	-- the first use of "x :="
	z := x
	x := 3
	return z


Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!