lua-users home
lua-l archive

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


It was thus said that the Great Sergey Kovalev once stated:
> It will not surprise people. They will use functions as before.
> "pure_function" is additional feature for special cases
> It could isolate possible side effects, due to miss typing or forget to
> define local variable or any other reasons.

  I use luacheck to prevent typos and forgetting to define local variables. 
Running luacheck over this:

function fromjulianday(jd)
  a = jd + 32044
  b = (4 * a + 3) // 146097
  c = a - (b * 146097) // 4
  d = (4 * c + 3) // 1461
  e = c - (1461 * d) // 4
  m = (5 * e + 2) // 153

  return {
    day   = e - (153 * m + 2) // 5 + 1,
    month = m + 3 - 12 * (m // 10),
    year  = b * 100 + d - 4800 + m // 10
  }
end

produces 

Checking jd.lua                                    21 warnings

    jd.lua:1:10: setting non-standard global variable fromjulianday
    jd.lua:2:3: setting non-standard global variable a
    jd.lua:3:3: setting non-standard global variable b
    jd.lua:3:12: accessing undefined variable a
    jd.lua:4:3: setting non-standard global variable c
    jd.lua:4:7: accessing undefined variable a
    jd.lua:4:12: accessing undefined variable b
    jd.lua:5:3: setting non-standard global variable d
    jd.lua:5:12: accessing undefined variable c
    jd.lua:6:3: setting non-standard global variable e
    jd.lua:6:7: accessing undefined variable c
    jd.lua:6:19: accessing undefined variable d
    jd.lua:7:3: setting non-standard global variable m
    jd.lua:7:12: accessing undefined variable e
    jd.lua:10:13: accessing undefined variable e
    jd.lua:10:24: accessing undefined variable m
    jd.lua:11:13: accessing undefined variable m
    jd.lua:11:27: accessing undefined variable m
    jd.lua:12:13: accessing undefined variable b
    jd.lua:12:23: accessing undefined variable d
    jd.lua:12:34: accessing undefined variable m

  I'm not aware of any tool that can check for global usage, but something
like luacheck could probably be written to report global and upvalue usage
by function.  

> Moreover it could be combined with common function to achieve different
> aims.
> The simples way is
> 
> pure_function fn(args)
> -- ...
> end
> 
> converted into something like this
> 
> isolate_all_upvalues(args, -- somehow remove all upvalues
> function fn(_ENV,args)
> -- ...
> end)

  Could you give a better example of this?  I'm not following the example
given.  Are you just collecting any upvalues into a custom environment for a
function?  

  -spc