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
safely.


For concreteness, "=" style:

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

function g()
	return x -- nonlocal
end

This is the new style witih ":=":

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

WARNING or ERROR from the compiler:

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

Other cases:

function f()
	return x -- works as before
end

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
end

Tom.


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/