lua-users home
lua-l archive

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


On Fri, Aug 14, 2009 at 5:00 AM, David Manura<dm.lua@math2.org> wrote:
> One of the prototypical problems in [1] was to implement a function this:
>
>  --Wraps a function with trace statements.
>  function trace(f)
>    return function(...)
>      print("begin", f)
>      local result = tuple(f(...))
>      print("end", f)
>      return result()
>    end
>  end
>
> Could we add new keywords that correspond with the TUPLE/DETUPLE
> opcodes to achieve something like the above?
>
> [1] http://lua-users.org/wiki/VarargTheSecondClassCitizen
>

In theory, yes, the new opcodes could be used to solve this problem.
One approach would be to use something akin to the Lua 4.0 syntax for
upvalues and prefix the variable name with a special symbol to show it
has special usage:

function trace(f)
  return function(...)
    print("begin", f)
    local %results = f(...)
    print("end", f)
    return %results
  end
end

In this situation, %name on the left hand side of an assignment, where
name is the name of a local variable, creates a tuple. %name anywhere
else destroys the tuple, yielding its values. If this syntax were to
be adopted, it would carry several restrictions:
* At any given scope level (i.e at any block, excluding its "child"
blocks) there can exist a maximum of one tuple variable at any one
time.
* A tuple variable must be always be destroyed exactly once.
* A tuple variable can only be destroyed at the scope level of its creation.
* You cannot do anything with a tuple variable other than destroy it.
* After a tuple has been destroyed, the local will contain nil.

If some other syntax were adopted to solve this problem, it would have
to carry similar restrictions to ensure that a TUPLE is always matched
by a DETUPLE, that tuples are destroyed in reserve order to that in
which they were created, and that a tuple is not used for anything
except DETUPLE.