Ability to detect subclassing (class B extends A) Topic is solved

Propose new features and changes
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Ability to detect subclassing (class B extends A)

06 Oct 2015, 07:19

I was thinking of a new meta-function that is called every time the extends keyword is used, something like:

Code: Select all

class A
{
	__Extend(SubClass)
	{
		/* You can do the ff here
		- check if the proper methods/properties were defined/overrriden
		- store a reference to the subclass
		- initialize some class properties
		- throw Exception if a private method is overrriden
		- etc.
		*/
	}
}

; using the 'extends' keyword, SuperClass.__Extend() is
; called(maybe before the auto-execute section) passing
; SubClass as argument
;
; or alternatively, SuperClass.__Extend.Call(SubClass) - 'this' is now a reference to the SubClass
class B extends A
{
	
}
Currently, something similar can be achieved using Instance variables, however, new must be used. Or another option is to instruct those who want to extend the class to declare a Static/Class variable and have it call a method defined in the base class. The former requires the subclass to be instantiated while the latter requires extra work form the user's end and can easily be neglected. Another way is to scan the script for class declaration(s) - this wouldn't work if the script was launched dynamically via AutoHotkey.exe *.

Unless there is some undocumented way to achieve this.
User avatar
RobertL
Posts: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: Ability to detect subclassing (class B extends A)

06 Oct 2015, 09:03

Code: Select all

class A{
	b:=new B(this)
}
class B{
	__New(subclass){
	}
}
I used like this. Could set base of subclass, also could add __Delete in B. And could static b:=new B(this).

Relative: [Close]AddRef on released object - Manage Refference Count After __Delete
我为人人,人人为己?
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Ability to detect subclassing (class B extends A)

06 Oct 2015, 17:00

I will consider it.

As a workaround, you could grab the global var list and find extended classes by checking ObjHasKey(var, "__Class") && var.base.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Ability to detect subclassing (class B extends A)

06 Apr 2018, 19:40

This can be mostly done with meta-functions (defined in a super-class, not the class itself), but requires methods and static properties to not actually be defined inside the class itself. For instance, the properties and methods can be defined in a separate internal class, with the "public" class extending the internal one (which can be a nested class; although that creates a circular reference, it would normally only be released on exit anyway). The internal class would extend the real super-class. The super-class would define meta-functions which would be invoked the first time the sub-class is accessed.

However, it is messy and error-prone, as meta-functions tend to be. I agree that the idea of __extend has merit, but I think something slightly more conventional could serve the same purpose: if __initClass is called for each class before first use (or on script startup), the class can perform whatever initialization it needs, including calling base.__initClass(). Instead of referring to SubClass, you would refer to this, under the condition that this != the_current_class.

If the pre-parser did the same with static class variables and __initClass as it does for instance variables and __init, static class variable initializers could naturally refer to this (the class itself). It might also save on code size. However, it would mean that a class can't define both static variables and __initClass. Proper documentation would also be needed to allow scripts to make legitimate use of it. (__Init is documented only vaguely because I thought I might change the implementation.)
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Ability to detect subclassing (class B extends A)  Topic is solved

02 Dec 2022, 19:13

v2 permits this via static __new(), where this may be a subclass or the class which defined the method. The base class is always initialized first.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 23 guests