lua-users home
lua-l archive

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

Patrick Donnelly <batrick.donnelly <at>> writes:
> Perhaps an inline function addition...I just find
> these constructs inelegant:
> local x = function(a, b) end
> function sort()
>   table.sort(sometable, x)
> end

The following adds only the overhead of a comparison test and provides the
advantage that the object is only constructed if sort is run at least once:

local x; function sort()
  x = x or function(a, b) end
  table.sort(sometable, x)

This applies similarly for tables or any other resource:

local is_valid
function test(x)
  is_valid = is_valid or {a=true,b=true,d=true}
  return is_valid[x]

Still, there could be advantages if it were allowed for a variable to be
syntactically declared inside a block but lexically scoped in a parent block:

function test()
  return (scope 2: {a=true,b=true,d=true})[x] -- only construct once

That's probably something for Metalua.