lua-users home
lua-l archive

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


I'm not fond of _javascript_ scope shenanigans. I think simple (explicit) is good in scope declaration and strict is our friend; though I don't often use strict and suffer the consequences.

_javascript_ scoping has recently greatly improved with using 'let' instead of 'var'.

In creating variables (or reading those that don't exist) I still don't get it, why default anything should ever be a good idea, it's just an invitation to bugs due to spelling mistakes.

And yes this applies to config files as well. I used Lua like this and got negative feedback of people complaining there wasn't any notification because they misspelled a variable. So instead after the user config file finished, I made a loop that checks if any unknown variables are in global space and error if so.

For another case, this is a simplified version for locking globals: 
---
local t = _G
local mt = getmetatable( t ) or { }
mt.__index = function( t, k )
if k ~= '_' and string.sub( k, 1, 2 ) ~= '__'
then
error( 'Access of non-existing global "' .. k ..'"', 2 )
else
rawget( t, k )
end
end

mt.__newindex = function( t, k, v )
if k ~= '_' and string.sub( k, 1, 2 ) ~= '__'
then
error( 'implicit global ', 2 )
else
rawset( t, k, v )
end
end

function lockGlobals( )
setmetatable( t, mt )
end
----
I don't remember why I made exceptions for _ and __ and if these are used to access _G in the first place.