Page 1 of 1
Can I self = NIL somehow in OOP?
Posted: Mon Dec 21, 2020 3:44 pm
by Bugala
Code: Select all
myOOP = {}
Function myOOP:Create()
Local o = {}
SetMetaTable(o, self)
self.__index = self
o.test = 3
Return(o)
EndFunction
Function myOOP:Destroyer()
self = Nil
EndFunction
a = MyOOP:Create()
DebugPrint("before destruction a.test: "..a.test)
a:Destroyer()
DebugPrint("after destruction a.test: "..a.test)
I have a situation where it would be handy to be able to erase current OOP using "self = Nil" or something similar, however, seems I am not able to get it to happen.
Is there a way to do this?
Re: Can I self = NIL somehow in OOP?
Posted: Mon Dec 21, 2020 5:03 pm
by SamuraiCrow
Do you get an error or just disappointment that the garbage collector isn't instant?
Re: Can I self = NIL somehow in OOP?
Posted: Mon Dec 21, 2020 5:14 pm
by Bugala
disappointment. That in this example case, it both times debugprints number 3, when I am hoping that the second time it would give me an error of both a as well as a.test not existing.
The actual reason for looking at this is that I am having in my program this list of OOPs that all simply do their update function.
as example:
Code: Select all
foreach(ListOfOOPs, function (id, content) content:UpDate() endfunction)
and idea is that all those OOPs are existing only temporarily, and when they have finished their jobs, they are supposed to cease to exist.
There are other ways to achieve this, but it seemed to me that the simplest had been that when the Update function notices their task is over, they could simply self=NIL. But this didn't seem to work as I hoped.
Re: Can I self = NIL somehow in OOP?
Posted: Mon Dec 21, 2020 7:04 pm
by jalih
Bugala wrote: ↑Mon Dec 21, 2020 5:14 pm
disappointment. That in this example case, it both times debugprints number 3, when I am hoping that the second time it would give me an error of both a as well as a.test not existing.
Why not just use?:
instead of:
Re: Can I self = NIL somehow in OOP?
Posted: Mon Dec 21, 2020 7:45 pm
by Bugala
jalih wrote: ↑Mon Dec 21, 2020 7:04 pm
Why not just use?:
instead of:
Because in the real version I don't have access to a, I don't know if it is a, b, or c, All I can access is "self", hence self = NIL, self = {}, self = 0 or something similar I am trying to achieve the result of getting it destroyed. I can get to know a, b, c... but it would be much easier if I could just use self.
Re: Can I self = NIL somehow in OOP?
Posted: Mon Dec 21, 2020 8:45 pm
by SamuraiCrow
The real problem with your example code is that the "a" variable is global and never goes out of scope. In order to be fully nullified, all uses of the handle to the table need to be Nil, not just one of them. Variable handles are reference counted so if one is forced to Nil, that handle will go out of scope but the remaining ones will still be valid. Does this make sense?
Re: Can I self = NIL somehow in OOP?
Posted: Mon Dec 21, 2020 8:57 pm
by Bugala
ah, so you mean that basically to use the a:Destroyer as example
"a" has two different references to it, "a" references to "a" as well as self references to "a".
so making self=NIL, will still keep "a" referencing to "a".
right?
Re: Can I self = NIL somehow in OOP?
Posted: Mon Dec 21, 2020 9:38 pm
by SamuraiCrow
Correct! Now you're getting it!
Re: Can I self = NIL somehow in OOP?
Posted: Tue Dec 22, 2020 9:37 am
by Bugala
Thanks, that is really helpful to understand this. Obviously self=NIL won't work then.