lua-users home
lua-l archive

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


Another thought...

Say there were a new statement of the form:

	 global var1, var2, var3

This would mean that we want to access the given names from the environment for the chunk.

Global declarations like this would have the same precedence in lookup as locals. Hence,

	local print = function( x ) io.write( x ) end -- [ 1 ]

	print( "Foo" ) -- Calls [ 1 ]

	global print

	print( "Baz" ) -- Calls print as defined in the chunk environment

One could then compile code with the insistence that all variables be declared as either local or global.

But now we can use this in conjunction with the _ENV variable techniques to create alternate global environments. When such an environment is in place -- i.e., a specific _ENV declaration has superseded the chunk environment -- then variable names that cannot be resolved as declared locals or globals are resolved as elements of _ENV.

So, now I can write:

	local _ENV = module()

	function say_hello()
		global print
		print( "Hello" )
	end

This seems like it points toward something useful, but I'm not sure it's there yet. For example, if I leave out the declaration "global print", this still compiles but now it goes looking for print in the module.

Mark