Ability to detect subclassing (class B extends A)

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

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] [Expand] [Download] GeSHi © Codebox Plus


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: 540
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] [Download] GeSHi © Codebox Plus

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: 6040
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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: 6040
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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.)

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 3 guests