lua-users home
lua-l archive

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




(I say "equivalent" because
of course it could be generating a special bound-function object
instead of a closure.) 

I've been daydreaming about whether or not such a special "bound-function object" might be practical.  A non-allocating implementation would would require 2 registers, one for the object and one for the method name.  After assigning `local x=y:z`, it would be easy to evaluate 'x(1,2,3)', but, things would start to fall apart if you wanted to pass x to another function, return it, or insert it into a table.  You could throw a syntax error in such cases, but, such a limited feature strikes me as more trouble than it would be worth.  

I'm still not convinced that this is NECESSARY, but if it were to be
implemented, I think this would be best.

A special parser hook is probably possible; provided that we're just talking about replacing instances of the invalid syntax 
"x=y:z" with
"function x(...) return y:z(...) end"

I don't think it's a good idea though. The current syntax has the advantage of clarity.  If you need terser code, probably better to write a helper function.  For example:

function metafunctions_to_closures(object)
  local new_object=shallow_copy(object)
  for k,v in pairs(getmetatable(object)) do
     if type(v)=='function' then
         new_object[k] = function(...) return v(new_object,...) end
     end
  end
  return new_object
end


-Sven