lua-users home
lua-l archive

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


2009/8/14 Doug Currie <doug.currie@gmail.com>:
>
> On Aug 14, 2009, at 12:00 AM, David Manura 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
>
>
> Would this work?
>
> function trace (f)
>  return function(...)
>    print("begin", f)
>    local function vals(...)
>      print("end", f)
>      return(...)
>    end
>    vals(f(...)...)
>  end
> end

You don't need the patch in that situation, since f(...) is the last
parameter of the call to vals, and therefore the result list is not
truncated. This works perfectly:

function trace (f)
 return function(...)
   print("begin", f)
   local function vals(...)
     print("end", f)
     return ...
   end
   return vals(f(...))
 end
end