lua-users home
lua-l archive

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



On 12 Oct 2006, at 07:33, Nick Gammon wrote:

Is this a bug?

No.


--------------
mt = {}
mt.__index = mt
mt.test = function (a, b, c)
          print "test"
          end  -- function mt.test

t = {}
setmetatable (t, mt)

ok, result = pcall (t:test) --> error: function arguments expected near `)'
--------------

The a:b syntax isn't valid as an expression, only as part of a function call. That means that (a:b)(x, y, z) can't be explained by considering (a:b) (production of a function value) and (x, y, z) (calling a function with specified args) separately. This is a shame in my opinion.

Years ago I suggested that a:b be sugar for

function (...)
  return a.b(a, ...)
end

Of course, we couldn't use '...' like that in those days. And also of course I'm not suggesting that a new function closure be created for the common case of a:b(x,y,z).

This would mean then that a:b would be a valid expression. I think your pcall case provides good motivation for this.

(I think it's a bit trickier than my simple rewrite suggests, as 'a' should probably be an upvalue of the created closure).

drj