How in Love or lua to throw any error if a variable is referenced without first declaring it as 'local'?
I'm over wasting hours working out why my variable returns nil only to find out I typed it wrong.
Is there a way to force variable declaration?
Google failed: how to force explicit variable declaration?
Google failed: how to force explicit variable declaration?
Last edited by togFox on Sun May 16, 2021 7:41 am, edited 1 time in total.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
Re: Google failed: how to force explicit variable declaration?
Tnere are several "strict mode" implementations that do this. Search for "strict.lua".
Re: Google failed: how to force explicit variable declaration?
I use Google Code with the "Love2D Support" extension by Pixelbyte Studios. That helps, but it's not perfect.
One of the things it does is flag variables that have not been declared local (unless the variable starts with an upper case letter.)
The thing is, Lua is designed not to need variable declarations, and there is no syntax to actually declare a variable - so even if you use LOCAL to identify local variables, there's no option to explicitly declare table variables or global variables. Your best bet is probably some sort of source code analysis tool.
You might also read up on this:
https://www.lua.org/pil/14.2.html
Also, look at this:
https://github.com/mpeterv/luacheck
And yes - this is the biggest problem I have with scripting languages that don't require or allow for variable declaration and explicit types: there's no way to tell whether a variable was mis-typed or the programmer intended to have things like "hold" and "hodl" as two separate variables. Don't get me started on the fact that you can't enforce parameter types in functions....
One of the things it does is flag variables that have not been declared local (unless the variable starts with an upper case letter.)
The thing is, Lua is designed not to need variable declarations, and there is no syntax to actually declare a variable - so even if you use LOCAL to identify local variables, there's no option to explicitly declare table variables or global variables. Your best bet is probably some sort of source code analysis tool.
You might also read up on this:
https://www.lua.org/pil/14.2.html
Also, look at this:
https://github.com/mpeterv/luacheck
And yes - this is the biggest problem I have with scripting languages that don't require or allow for variable declaration and explicit types: there's no way to tell whether a variable was mis-typed or the programmer intended to have things like "hold" and "hodl" as two separate variables. Don't get me started on the fact that you can't enforce parameter types in functions....
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Google failed: how to force explicit variable declaration?
^two things:
- at least with the local keyword, you can declare what variable name should be local to what scope even without setting it at that point:
Above example, bleh will be local to the file scope at least, and won't be a global that way.
- You can enforce parameter types in functions with the aptly named type function lua provides you... it is a bit of a hassle though:
- at least with the local keyword, you can declare what variable name should be local to what scope even without setting it at that point:
Code: Select all
local bleh
function meh()
bleh = new('foo')
end
function bar()
bleh:something() -- can still error if meh wasn't called first...
end
- You can enforce parameter types in functions with the aptly named type function lua provides you... it is a bit of a hassle though:
Code: Select all
function asdf(a,b,c)
if type(a) ~= 'number' then return end
if type(b) ~= 'string' then error('b not string') end
if type(c) == 'table' then
-- ...
elseif type(c) == 'function' then
-- ...
end
-- now do something actually
end
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: Google failed: how to force explicit variable declaration?
strict.lua is run-time. I prefer to detect those statically as you don't depend on the code path being executed in order to detect it.
I use my own method to detect globals: https://love2d.org/forums/viewtopic.php?f=5&t=86717
If using LuaPreprocess, you can also make the functions so that they report the name of the parameter in the error message and you can also easily remove the checks in the release version.
I use my own method to detect globals: https://love2d.org/forums/viewtopic.php?f=5&t=86717
You can make it easier for you:
Code: Select all
function force(var, typ)
if type(var) ~= typ then error("parameter is not a " .. typ) end
end
function forceany(var, ...)
for i = 1, select('#', ...) do
if type(var) == select(i, ...) then return end
end
error("parameter is not one of " .. table.concat({...}, ", "))
end
function asdf(a,b,c)
force(a, "number")
force(b, "string")
forceany(c, "table", "function")
-- ...
end
Who is online
Users browsing this forum: BOTTOMFRAGGER523 and 7 guests