[10 May 2008] Using the colon instead of the period as link in function call

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
User avatar
TheMartian
Posts: 109
Joined: Sun Feb 28, 2010 12:51 pm

[10 May 2008] Using the colon instead of the period as link in function call

Post by TheMartian »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Sat, 10 May 2008 18:00:37 -0000

Hi

While reading on page 22, column 2 in Amiga Future issue 72 about the latest 3.1 update for Hollywood I noticed that I could now use a colon instead of a period as link in a function call.

Can I ask some one to provide a small piece of code demonstrating how this is supposed to work. I have not seen (or missed it anyway) any references to this in the online manual.

Currently I use this approach to minimize typing and speed up the code...

Code: Select all

ItemList = {}
ItemList[0] = {}

Function Doh()
  local il = ItemList[0]
  il.name$ = "Some name"
  il.color = some_color
/* etc. */
EndFunction
and so on.... which works quite well. But perhaps that colon thing can be used to do something similar in a more 'elegant' way :-)

Thanks

Jesper
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[11 May 2008] Re: Using the colon instead of the period as link in function call

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Sun, 11 May 2008 12:23:48 +0200
Hi

While reading on page 22, column 2 in Amiga Future issue 72 about the latest 3.1 update for Hollywood I noticed that I could now use a colon instead of a period as link in a function call.

Can I ask some one to provide a small piece of code demonstrating how this is supposed to work. I have not seen (or missed it anyway) any references to this in the online manual.

Currently I use this approach to minimize typing and speed up the code...

Code: Select all

ItemList = {}
ItemList[0] = {}

Function Doh()
 local il = ItemList[0]
 il.name$ = "Some name"
 il.color = some_color
/* etc. */
EndFunction
and so on.... which works quite well. But perhaps that colon thing can be used to do something similar in a more 'elegant' way :-)
I think you misunderstood the function of the colon. The colon is used to call methods with an invisible "self" parameter. This behaviour is currently undocumented in the Hollywood guide, but you can read up on it here: http://www.lua.org/pil/16.html
User avatar
TheMartian
Posts: 109
Joined: Sun Feb 28, 2010 12:51 pm

[11 May 2008] Re: Using the colon instead of the period as link in function call

Post by TheMartian »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Sun, 11 May 2008 20:10:09 -0000

Ahh. Thanks. I get it...

As an example this does the trick:

Code: Select all

ItemList = {}
  ItemList.myNumber = 2
  ItemList.double_doh = Function(self,n) self.myNumber = 
self.myNumber*2 Return(self.myNumber) EndFunction

nprint(ItemList:double_doh(10))
Doing it this way the 'self' can be replaced by any identifier it seems. I tried 'cheese' instead of 'self', and it still worked :-)

regards Jesper
Locked