lua-users home
lua-l archive

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

 On 9/29/2010 2:47 PM, HyperHacker wrote:
>>  There are no confusing similar concepts. Dot is "get value" and
>>  colon is "call". Easy to grasp, isn't it?
>  But dot also means call.
It's a matter of perspective. Both retrieve the value; one returns it
(and you might immediately call afterward); the other is only valid if
you're calling it in the same expression.
Like so:

a.doit       -- get value
a.doit()     -- call with no parameters
a:doit()     -- call with implied parameter a
a:doit -- invalid (though could just as logically be equivalent to a.doit)

My argument is that the semantic difference between the second and third is small; that the eye will easily mistake a dot and a colon; and that can lead to difficult to find errors, especially (but not only) for those who also use languages where there is no difference between the dot usage and the colon usage. My conclusion is that replacing the colon with something that less resembles a dot would improve the situation.

I like the idea myself of declaring a function differently such that
the 'self' parameter is automatic without needing to use a colon.
That would be my preferred solution.

However it has some potential ambiguity:
method a.b() print(self) end
a.b() --should print the address of a
n = a.b
n() --???
I may be completely missing the subtlety of these examples, but in this case, I would expect n() to print the address of n, because n now refers to "print(self)" and self here is n.
c = {b=a.b, d=a.b, e=n}
c.b() --prints a, or c?
c.d() --prints a, or c?
c.n() --???
Here I would expect c, c, and c for the same reason, assuming when you wrote c.n() you meant c.e().

c.b() is the same as c:b(), which (in unaltered Lua) is equivalent to c.b(c), so self would be c.