Page 1 of 1

how to reference class/subclass object inside instance method?

Posted: 25 Oct 2020, 07:50
by sirksel
I'm writing a class that is intended to be subclassed. Inside that class, I'm writing an instance method. Inside this instance method, I need to refer to a static variable of the class (or more likely, subclass) from which the instance was derived. How do I reference the subclass generically? (Basically, I'm looking for the equivalent of this from a static method, but I'm inside of an instance method.)

Code: Select all

class master {
  static x := 'default value'
  mbx() => msgbox(master.x)  ;I want to refer to the class/subclass, not master.
}
class subclass extends master {
  static x := 'overridden value'
}
obj := subclass.new()
obj.mbx()  ;I want this to display msgbox of the overridden x value
I'm sure I've just overlooked something in the docs, but I'd appreciate any help you can give. Thanks.

Re: how to reference class/subclass object inside instance method?

Posted: 25 Oct 2020, 14:08
by kczx3
You might have to manually assign to master.x inside __new of the subclass

Re: how to reference class/subclass object inside instance method?  Topic is solved

Posted: 25 Oct 2020, 15:02
by swagfag

Code: Select all

%this.__Class%.x

Re: how to reference class/subclass object inside instance method?

Posted: 25 Oct 2020, 20:13
by kczx3
Nice 👍🏻

Re: how to reference class/subclass object inside instance method?

Posted: 25 Oct 2020, 23:15
by sirksel
Worked perfectly. Thanks to both of you for responding!

Re: how to reference class/subclass object inside instance method?

Posted: 29 Oct 2020, 04:27
by Helgef
There is a better way, described in this topic: :arrow: [v2 - a108][Tips] How to create a Class property for instances of a class.

Cheers.

Re: how to reference class/subclass object inside instance method?

Posted: 30 Oct 2020, 01:34
by sirksel
Great writeup, @Helgef. Sorry I missed it before posting this thread. Quick question, extending your logic/examples, would it be bad form or bad performance hit to DefineProp to add a getter property to the Object prototype more generally that basically is just class => %this.__Class%?

Since we have this and super available in method/property bodies, it would sure be cleaner to have class available too. I know it's a breaking change that might conflict with existing var names, it's a "nice to have", etc. I'm sure you guys have already thought about it and declined for good reason... Thanks again!

Re: how to reference class/subclass object inside instance method?

Posted: 31 Oct 2020, 07:10
by Helgef
class => %this.__Class% is ofc better than using %this.__Class% directly, it is still worse than my proposal though. Imo, a class property should be built-in. I think lexikos mentioned somewhere that he had considered it, or perhaps it was available in objects.ahk.

Cheers.

Re: how to reference class/subclass object inside instance method?

Posted: 15 Nov 2020, 20:40
by sirksel
@swagfag and @Helgef, quick follow-up question... I was reading another thread answer you wrote (related to GUIs) that reminded me how circular references can make objects indestructible without first manually removing the references.

As it relates to the question in this thread, I've added a "class object" property to all objects (by extending the Object prototype). The property is pretty simple and looks like this:

Code: Select all

clsobj => %this.__class%   ;class object
Does this property's existence (or calling it from subclassable classes to invoke static methods from inside of instance methods) interfere with automatic destruction? I didn't think it would, but after reading the other thread, now I'm second-guessing myself.

Re: how to reference class/subclass object inside instance method?

Posted: 17 Nov 2020, 23:14
by swagfag
there is no automatic destruction for class objects. their lifetime ends when the script closes. whether a circular reference is created through the use of such a property depends on what u end up doing with the reference.

since destructors dont run for classes(not that u should be writing static ones in the first place anyway)
__Delete is not called for any object which owns a property named __Class.
the worst thing u could do is exhaust ur system memory and even so, ud probably notice something was very very very wrong with ur script long before that ever happens
@Helgef i dont get what ur gripes with %this.__Class% are nor is it obvious from the other thread. can u clarify?

Re: how to reference class/subclass object inside instance method?

Posted: 20 Nov 2020, 21:25
by sirksel
@swagfag this was very helpful. After reading your response, my more focused question then, is by creating a reference to the class of an instance (inside an instance method), does that prevent instances themselves from being destroyed? Based on your answer, I'm thinking the answer is no for two reasons:
1. When the instance method's execution ends, the reference falls out of scope, and so there is no remaining reference to the class. (I know you can't see the code, but it's just a bunch of calculations for which the values of some static properties are required, hence the need for %this.__Class%.)
2. Because the class and prototype definitions are distinct from instances, there's no real worry anyhow, since (as you point out) destructors don't run for classes.
Let me know if I interpreted any of that wrong... Thanks again for the help!

Re: how to reference class/subclass object inside instance method?

Posted: 29 Nov 2020, 09:07
by Helgef
does that prevent instances themselves from being destroyed?
no it does not. I don't get your interpretation though. Your code,

Code: Select all

clsobj => %this.__class%   ;class object
doesn't create any circular (or any) references.
i dont get what ur gripes with %this.__Class%
It relies on a super global variable which means,
1) it doesn't work for nested classses,
2) the variable might have been "deleted",
3) the variable might be a local variable (or perhaps in the future a variable in another module)

Cheers.