The responses were very helpful. Thank you guys.
In your second example (printMyClass) you’ve called the functions using the dot operator rather than the colon, but the function uses self so it will return the error: attempt to index local ‘self’ (a nil value)
It should be:
a:printMyClass() b:printMyClass() c:printMyClass() d:classPrinter()
Corrected, above.
Sheesh, you guys make everything so complicated!!! And, what’s worse: some things said above are plain wrong!
It can be summed up much, much simpler:
Using the : or colon operator is exactly the same as using a dot operator, but with an extra parameter “self” as the first parameter in the function.
So, yes, these two are exactly the same expressions:
object:eatFruit(“banana”)
object.eatFruit(self, “banana”)
Or in the same vein, these two expressions are also the same:
object:setFillColor(0,1,1)
object.setFillColor(self, 0,1,1)
You can actually freely interchange the colon with self argument, and the dot without the self argument.
That’s basically all there is to it.
Regarding method and property: the dot is used for both, so the poster saying that dot was for properties, not methods, was wrong.
Small addition: the colon is only used for methods (or functions, if you prefer that terminology).
Another small addition: the “self” parameter is very useful when you start using object oriented programming, or pseudo-OOP, as it is sometimes referred to in Lua / Corona. The self parameter, as said above, refers to the object on the left of the line of code - this is the object that is calling the function.
For absolute clarity, the code below looks like “calling” the function, not “defining” the function. When calling the function, self isn’t defined, the object is so:
object.eatFruit(self, "banana")
should be:
object.eatFruit(object, "banana")
Inside the code for eatFruit, the first parameter is called “self”. So when defining the function:
function object.eatFruit( self, fruit ) function object:eatFruit( fruit ) print( self, fruit ) end
Both function definitions are identical and you access “self” within the function.
Rob
Very true, Rob.
If I may give a tip: I always use the DOT notation when writing my own code. While for pros it doesn’t matter that much, I think for beginners to intermediate coders it helps to see what is being passed as parameters, and where it is passed.
opinions will legitimately differ on this, but… the colon is just sugar, but then so is the dot. a “purist” might even argue that the form t"f" would thus be the preferred way to word t.f(t) or t:f() because then the internal behavior is fully revealed by the syntax. (i myself would rather use the sugar)
Hi Dave,
Very much a matter of preference of course, so there is no right or wrong way. I did and still do like, however, the fact that all passed arguments are “visible” when I read my code, hence the dot notation for me personally.
Also, since all my code is modular, my code will always read like the contents of a table:
myClass.aCertainFunction = function(self, argument1, argument2) -- doSomethingHere end
Thanks for the very informative responses guys.
You’re welcome. In case you’re venturing down that path, I found Lua’s flavour of object oriented programming to be very easy and intuitive to use and learn with the exception of a few small things. Once you’ve gotten past these simple obstacles (with the help of the forum) it’ll help your code reach a higher level in no time!