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

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
sirksel
Posts: 101
Joined: 12 Nov 2013, 23:48

how to reference class/subclass object inside instance method?

25 Oct 2020, 07:50

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.
User avatar
kczx3
Posts: 1254
Joined: 06 Oct 2015, 21:39

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

25 Oct 2020, 14:08

You might have to manually assign to master.x inside __new of the subclass
sirksel
Posts: 101
Joined: 12 Nov 2013, 23:48

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

25 Oct 2020, 23:15

Worked perfectly. Thanks to both of you for responding!
sirksel
Posts: 101
Joined: 12 Nov 2013, 23:48

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

30 Oct 2020, 01:34

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!
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

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

31 Oct 2020, 07:10

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.
sirksel
Posts: 101
Joined: 12 Nov 2013, 23:48

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

15 Nov 2020, 20:40

@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.
swagfag
Posts: 4138
Joined: 11 Jan 2017, 17:59

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

17 Nov 2020, 23:14

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?
sirksel
Posts: 101
Joined: 12 Nov 2013, 23:48

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

20 Nov 2020, 21:25

@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!
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

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

29 Nov 2020, 09:07

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.

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 3 guests