Page 1 of 1

Google failed: how to force explicit variable declaration?

Posted: Sun May 16, 2021 4:02 am
by togFox
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?

Re: Google failed: how to force explicit variable declaration?

Posted: Sun May 16, 2021 4:08 am
by grump
Tnere are several "strict mode" implementations that do this. Search for "strict.lua".

Re: Google failed: how to force explicit variable declaration?

Posted: Sun May 16, 2021 5:04 am
by tomxp411
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....

Re: Google failed: how to force explicit variable declaration?

Posted: Sun May 16, 2021 10:32 am
by zorg
^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:

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
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:

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

Re: Google failed: how to force explicit variable declaration?

Posted: Sun May 16, 2021 11:16 am
by pgimeno
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

zorg wrote: Sun May 16, 2021 10:32 am - You can enforce parameter types in functions with the aptly named type function lua provides you... it is a bit of a hassle though:
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
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.