What is the difference between a.f(x) and a:f(x)?

a.f simply means 'look up f in a'. If the result is 'callable' then you can call it. A common pattern is to keep functions in tables, which is the approach taken by the standard libraries, e.g. table.insert etc.

a:f actually has no meaning on its own. a:f(x) is short for a.f(a,x) - that is, look up the function in the context of the object a, and call it passing the object a as the first parameter.

Given an object a, it is a common error to call a method using .. The method is found, but the self parameter is not set, and the method crashes, complaining that the first parameter passed is not the object it was expecting.



Back