lua-users home
lua-l archive

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


Hi, all!

Lua 5.1.

Sometimes I need to write a function like this:

 foo = function(a1)
   return function(a2)
     print(a1, a2.data)
   end
 end

This is useful when creating small DSLs with entries like this:

 foo "name"  { data = 123 }

Nesting of this functions is usually 2-3 levels. Also I found useful
to validate argument types in this functions. I use pipeline-generator
function:

 name_data = function(sink)
   assert(type(sink) == "function")
   return function(name)
     assert(type(name) == "string")
     return function(data)
       assert(type(data) == "table")
       return sink(name, data)
     end
   end
 end

So I can write more readable function definition:

 foo = name_data(function(name, data)
   print(name, data.123)
 end)

But I need many pipeline-generation functions, for different types and
number of arguments. Is there a way to build them in runtime? There
are no strict limitations on wrapper (like name_data) creation-time
overhead, but call overhead on the wrapped function (foo in this case)
should be minimal.

There is obvious solution:

 make_wrapper = function(types)

   local n = #types
   for i = 1, n do
   end
 end