I have seen multiple points where people have used "." in their programs and ":" in their programs, usually the latter for calling functions in tables (like a file handle). Usually, i was able to use either and thought they were identical in the syntax, but I recently came across this thread and the solution requires ":" otherwise I get a "index expected, got nil" error. So I was wondering what the syntactical difference was, and the advantages to using one over the other. Thanks for the explanation in advance!
Whats the difference between "." and ":"?
Started by TheWhoAreYouPerson, Dec 21 2013 09:09 PM
6 replies to this topic
#1
Posted 21 December 2013 - 09:09 PM
#2
Posted 21 December 2013 - 09:34 PM
The colon is syntactic sugar. These are the same:
object:method("argument")
object.method(object, "argument")
#3
Posted 21 December 2013 - 10:22 PM
Yeah. The : passes the table the function belongs to as it's first argument. It can make code look a bit better (among other things)
myTableForAnObject.someFunction(myTableForAnObject, "some parameter")V.S.
myTableForAnObject:someFunction("some parameter")
#4
Posted 22 December 2013 - 12:40 AM
Thank you for the clear explanation! I was always wondering why people did that.
#5
Posted 22 December 2013 - 07:44 PM
So this means that string.length("HELLO") and "HELLO":length() are the same, besides the fact I could just use #?
Edited by Death, 22 December 2013 - 07:44 PM.
#7
Posted 23 December 2013 - 01:01 AM
Okay, sorry about that. I was wondering if I had to do that, but internet broke and I couldn't edit the post.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users












