0

I'm finding a way to note types variables and function arguments in Lua. Is there a way? And any LINT-like tool to check those types?

Sam
  • 7,252
  • 16
  • 46
  • 65
eonil
  • 83,476
  • 81
  • 317
  • 516
  • http://lua-users.org/wiki/LuaTools -> Code Documentation section, and http://lua-users.org/wiki/LuaLint. Not sure if these are the type of thing you are looking for... – Merlyn Morgan-Graham Jun 11 '11 at 07:38

1 Answers1

3

I don't like encoding types on variable names. I prefer giving the variables explicit enough names so their intent is clear.

If I needed more than that, I use a typechecking function when needed:

function foo(array, callback, times)
  checkType( array,    'table',
             callback, 'function',
             times,    'number' )
  -- regular body of the function foo here

end

The function checkType can be implemented like this:

function checkType(...)
  local args = {...}
  local var, kind
  for i=1, #args, 2 do
    var = args[i]
    kind = args[i+1]
    assert(type(var) == kind, "Expected " .. tostring(var) .. " to be of type " .. tostring(kind))
  end
end

This has the advantage of properly raising an error on execution. If you have tests, your own tests will do the LINT-stuff and fail if a type is unexpected.

kikito
  • 51,734
  • 32
  • 149
  • 189