Google failed: how to force explicit variable declaration?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Google failed: how to force explicit variable declaration?

Post 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?
Last edited by togFox on Sun May 16, 2021 7:41 am, edited 1 time in total.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

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

Post by grump »

Tnere are several "strict mode" implementations that do this. Search for "strict.lua".
User avatar
tomxp411
Prole
Posts: 29
Joined: Thu Apr 08, 2021 5:41 pm

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

Post 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....
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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
Me and my stuff :3True 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.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests