Page 1 of 1
why table.function doesnt pass var while table:function does?
Posted: Fri Sep 27, 2019 1:17 pm
by Bugala
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?
Re: why table.function doesnt pass var while table:function does?
Posted: Fri Sep 27, 2019 2:40 pm
by p-OS
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...
Re: why table.function doesnt pass var while table:function does?
Posted: Fri Sep 27, 2019 3:45 pm
by Bugala
Yes, you are right on that. Now I see why it does that.
Because I use:
It always assume I have self when calling it. If I use
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!
Re: why table.function doesnt pass var while table:function does?
Posted: Mon Sep 30, 2019 7:33 am
by Clyde
This link is useful for that kind of questions (I just discovered this last week):
http://lua-users.org/wiki/ObjectOrientationTutorial