lua-users home
lua-l archive

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


On Wed, Jul 3, 2019 at 10:56 AM Dirk Laurie wrote:
> We have much bigger problem in Lua: every typo in a local variable's name silently converts it to global.

The fix for that is well known and widely distributed under the name strict.lua.



Indeed? 
Try to use strict.lua to find a typo in this code:

   for number = 11, math.huge do
      local sum = 0
      for divisor = 1, number do
         sum = sum + (number % divisor == 0 and divisor or 0)
      end
      if sum/number == 1.8 then
         print("Friend of 10 is "..mumber)   -- the typo is here
         break
      end
   end



There is a problem with strict.lua: you have to write tests to cover all code paths.
Branching in Lua code is possible due to "while/repeat/for", "if/then/else", "goto", and due to short-circuit evaluation of expressions (such as "x = y or z").
Covering all branches by tests is either labour-consuming or impossible (see the example above).

The typo-in-locals problem could be solved only by an external parser/analyzer (luacheck?).
So why not delegating "to-be-closed variables assignment" problem to the same external parser/analyzer?
Particularly taking into consideration that non-constant to-be-closed variables might also be useful in Lua.