Difference Between . And :

Wondering what the difference between text.y and text:y is?

please dont be focused on the word TEXT but rather Im wondering what difference between the period . and the semicolon : in code?

I have yet to understand why and where to properly use them in a given coding scenario. Any help appreciated. Thank you.

  1. your example of text:y is a bad example, not because of the word “text”, but because “y” is a property, and not a method or function, and the : sign can only be used with methods or functions (for clarity, methods and functions are two different words for the same thing, not two different things).

  2. using : means, exactly and literally, the same as using . with a function, but adding the object calling the function as a first argument in between the brackets of the function. This means that [lua]myObject:start()[/lua] is EXACTLY the same as writing [lua]myObject.start(myObject)[/lua]

This is handy, because for some functions you need to tell which object should be affected, and becomes even handier when you start using object oriented programming concepts such as the term “self”.

Are you going into OOP? If so, some extra info will be helpful to you.

p.s. Because I was a bit confused with both ways of writing my code, I just decided to not use : at all, and always use . and add the object calling the function as a parameter manually. It just makes things clearer and more logical to understand what exactly is going on under the hood. Once you grasp this concept fully you can then move on to using : but you don’t have to at all.

When you use the colon ‘:’ in Lua, you are passing that Lua object as the first argument into its function.  It is syntactic sugar to make your Lua code look more object orientated like.

For example, the following 2 lines of code in Lua are equivalent…

myObject:call(x)

myObject.call(myObject, x)

Here is another example in Lua using a Corona function…

– Create a rectangle.

local myRect = display.newRect(0, 0, 100, 100)

– Normally you call the object’s function this way.

myRect:setFillColor(255, 0, 0)

– But you can also call the object’s function this way too.

myRect.setFillColor(myRect, 255, 0, 0) 

@jgcthatsme, you might find this thread helpful:

http://developer.coronalabs.com/forum/2011/12/13/or-self-equivalent#comment-74818

Naomi

Of course my post is a bad example, I dont know what im doing lol…that is until i read your post. Thank you for your help, I appreciate it much. Same for everyone else too. Great resources/links. I’ll remember you when i get to the top!^^

oh and no, im not planning on any really object oriented stuff, just training to be a corona ninja!

Thanks again. :slight_smile:

  1. your example of text:y is a bad example, not because of the word “text”, but because “y” is a property, and not a method or function, and the : sign can only be used with methods or functions (for clarity, methods and functions are two different words for the same thing, not two different things).

  2. using : means, exactly and literally, the same as using . with a function, but adding the object calling the function as a first argument in between the brackets of the function. This means that [lua]myObject:start()[/lua] is EXACTLY the same as writing [lua]myObject.start(myObject)[/lua]

This is handy, because for some functions you need to tell which object should be affected, and becomes even handier when you start using object oriented programming concepts such as the term “self”.

Are you going into OOP? If so, some extra info will be helpful to you.

p.s. Because I was a bit confused with both ways of writing my code, I just decided to not use : at all, and always use . and add the object calling the function as a parameter manually. It just makes things clearer and more logical to understand what exactly is going on under the hood. Once you grasp this concept fully you can then move on to using : but you don’t have to at all.

When you use the colon ‘:’ in Lua, you are passing that Lua object as the first argument into its function.  It is syntactic sugar to make your Lua code look more object orientated like.

For example, the following 2 lines of code in Lua are equivalent…

myObject:call(x)

myObject.call(myObject, x)

Here is another example in Lua using a Corona function…

– Create a rectangle.

local myRect = display.newRect(0, 0, 100, 100)

– Normally you call the object’s function this way.

myRect:setFillColor(255, 0, 0)

– But you can also call the object’s function this way too.

myRect.setFillColor(myRect, 255, 0, 0) 

@jgcthatsme, you might find this thread helpful:

http://developer.coronalabs.com/forum/2011/12/13/or-self-equivalent#comment-74818

Naomi

Of course my post is a bad example, I dont know what im doing lol…that is until i read your post. Thank you for your help, I appreciate it much. Same for everyone else too. Great resources/links. I’ll remember you when i get to the top!^^

oh and no, im not planning on any really object oriented stuff, just training to be a corona ninja!

Thanks again. :slight_smile: