This is not a big deal really, just to keep code bit cleaner.
But just today there came a situation where I made this function, which is accessed from several different places of the code.
That function is pretty self reliant, that all you have to do is call that function, and it does what it is supposed to do without needing any additional info.
Except for one thing. It needed one variable. I didnt need to know the state of that variable except inside the function. So basically that variable wouldnt need to exist anywhere else but inside that function.
However, I had to set it Gloval variable so it doesnt disapear everytime that function disappears, as it keeps saving false/true state of something which the function uses.
In practice, my solution was to add one variable to the beginning of code. However, if someone else reads that code, I need to add comment to it to explain that it is solely used by that one function, and nowhere else.
What I would have preferred is to having some sort of "userdata" like buttons have for a function. That way I could have simply set that variable as that function userdata, and everytime function is called, I would simply call that functions userdata to see the state of that variable, and if necessary, change it into different one.
This way code had stayed bit cleaner as if someone would be reading that code, he would only need to read that function and know exactly what that function is doing, instead of having to go and check where that variable is being initialized the first time.
It is a very little thing, but just came to my mind and in case it is easy to implement, then why not.
userdata for functions.
Re: userdata for functions.
Another helpful option would be to have some sort of "First Time" place in Function, which means that the first time the function is executed, it would do something it doesnt do after that anymore. as example:
Code: Select all
function p_myfunc()
START FIRSTTIME:
global myvariable=TRUE
END
if myvariable = true and something then myvariable = false
if myvariable = false and something else then myvariable = true
endfunction
- airsoftsoftwair
- Posts: 5446
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: userdata for functions.
I'm afraid these requests are all too hackish Maybe you want to wrap your function inside a little pseudo-class with a constructor and table private data. This could easily workaround these problems.
Re: userdata for functions.
I was afraid so.
Thanks from that pseudo calss tip, that didnt come to my mind, but it could work as a solution, when i just first recall those class making things how they were done (still have been sticking to old habits and not using them)
Thanks from that pseudo calss tip, that didnt come to my mind, but it could work as a solution, when i just first recall those class making things how they were done (still have been sticking to old habits and not using them)
- TheMartian
- Posts: 109
- Joined: Sun Feb 28, 2010 12:51 pm
Re: userdata for functions.
Hi
Check out the LUA manual here : http://www.lua.org/pil/16.4.html.
There are some examples on making 'private' properties and methods. I think this may be what you want.
regards
Jesper
Check out the LUA manual here : http://www.lua.org/pil/16.4.html.
There are some examples on making 'private' properties and methods. I think this may be what you want.
regards
Jesper
Re: userdata for functions.
thanks for link. Have to check i some time.