Can I self = NIL somehow in OOP?

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

Can I self = NIL somehow in OOP?

Post 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?
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Can I self = NIL somehow in OOP?

Post by SamuraiCrow »

Do you get an error or just disappointment that the garbage collector isn't instant?
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can I self = NIL somehow in OOP?

Post 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.
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Can I self = NIL somehow in OOP?

Post 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?:

Code: Select all

a = Nil
instead of:

Code: Select all

a:Destroyer()
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can I self = NIL somehow in OOP?

Post by Bugala »

jalih wrote: Mon Dec 21, 2020 7:04 pm
Why not just use?:

Code: Select all

a = Nil
instead of:

Code: Select all

a:Destroyer()
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.
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Can I self = NIL somehow in OOP?

Post 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?
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can I self = NIL somehow in OOP?

Post 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?
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Can I self = NIL somehow in OOP?

Post by SamuraiCrow »

Correct! Now you're getting it!
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can I self = NIL somehow in OOP?

Post by Bugala »

Thanks, that is really helpful to understand this. Obviously self=NIL won't work then.
Post Reply