lua-users home
lua-l archive

Re: Undefined variables returning nil

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


On Mar 19, 2013, at 9:23 PM, William Sumner <prestonsumner@me.com> wrote:
> Lua's behavior of silently returning nil for undefined variables can lead to bugs that propagate undetected. Has thought been given to changing this in a major version update, or are there reasons for retaining this behavior? I'm aware of strict.lua, but it only provides safety for global/module scope.
Something like this perhaps?
do
 local error = error
 local print = print
 local _ENV = setmetatable( {}, { __index = function( self, aKey ) error( ( 'Undefined %q' ):format( aKey ) ) end } )
 foo = 1
 print( 1, foo )
 print( 2, bar )
end

AltStyle によって変換されたページ (->オリジナル) /