why table.function doesnt pass var while table:function does?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

why table.function doesnt pass var while table:function does?

Post by Bugala » Fri Sep 27, 2019 1:17 pm

I encountered a strange situation and I have no idea why.

Code: Select all

 
Function s_Intro:Parser_RemoveUnwantedChars(string)
	DebugPrint("string on arrival: "..string)
endfunction
using:

Code: Select all

self:Parser_RemoveUnwantedChars("asd")
 
works.

But using:

Code: Select all

self.Parser_RemoveUnwantedChars("asd")
(difference being using . or : for tables function call)
Doesnt work. Why?

p-OS
Posts: 112
Joined: Mon Nov 01, 2010 11:56 pm

Re: why table.function doesnt pass var while table:function does?

Post by p-OS » Fri Sep 27, 2019 2:40 pm

I think, that if you create a method (:), HW implicitly adds a self Parameter:

Function s_Intro:Parser_RemoveUnwantedChars(string)
<=>
s_Intro["Parser_RemoveUnwantedChars"]=Function (self,string)

I assume you get an error that Parameters are missing...

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: why table.function doesnt pass var while table:function does?

Post by Bugala » Fri Sep 27, 2019 3:45 pm

Yes, you are right on that. Now I see why it does that.

Because I use:

Code: Select all

function table:Function(string)
It always assume I have self when calling it. If I use

Code: Select all

table.function(string)
then it is actually expecting to get two variables, where the first one will work as "self".

Now that I am sending only string, it thinks this string is self, and there is no string, string hence being NIL.

It makes sense now. Didnt realise it works this way. I thought if calling with table.function, it would then do the same, but just without possibility to call self, didnt realise it then assumes the first one is self if I originally declared the function with ":".

Good to know that now. Never realised that before. Thanks p-OS!

User avatar
Clyde
Posts: 198
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: why table.function doesnt pass var while table:function does?

Post by Clyde » Mon Sep 30, 2019 7:33 am

This link is useful for that kind of questions (I just discovered this last week): http://lua-users.org/wiki/ObjectOrientationTutorial
Currently using: Hollywood 8 with Windows IDE

Post Reply