@nnnik
There are more "cooperative" ways to extend array functionality than to
replace the Array class or redefine the constructor/function.
It isn't supported or recommended to "overwrite all instances of the class".
If "all instances of array should be of the class MyArray", what about MyArray itself or any other subclass, instances of which are also instances of array?
According to your statement, if you
Code: Select all
Array := MyArray
MyArray := UrArray
shouldn't "all instances of MyArray be of the class UrArray" and therefore "all instances of array be of the class UrArray"? Where does it end? This isn't how assignments and object references work.
Would you be happier if Array() was renamed to SquareBrackets()? It only serves as the implementation of
[], and a means to override that operator. If you redefine it, there's no guarantee that it will return an
Array. Above all, that's how it's been since v1.1. It was left this way
for now because it would be a low priority change, and because I think the drawbacks of making
[] 100% equivalent to
Array.new() outweigh the benefits.
Yes,
if the script redefines basic built-in functions, it will break standard expectations about what those functions do. That's a given. Changing expectations is more or less the purpose of redefining a function. At the very least, you're probably breaking the expectation that the object will have a known limited set of properties/methods. If you don't want to break expectations, don't do it.
#Warn ClassOverwrite should
probably always be used, and should not be ignored.
lexikos wrote: ↑07 Apr 2018, 20:39
I might consider making class names read-only identifiers (not variable) in v2. It would not reduce flexibility much since one can still clone the class and modify the original object as needed.
lexikos wrote: ↑14 May 2019, 20:43
I will probably do this, or make #Warn ClassOverwrite the default, or make it mandatory behaviour, to be reimplemented later as constants.
Of course, there's still no guarantee
Array.new() will return an
Array, since you can redefine
Array.new().
I cannot comprehend as to why we would overwrite all instances of array
Me neither.
If you want to overwrite all instances of the class it makes sense to overwrite the instance of the class.
"If you want to eat all apples, it makes sense to eat an apple." I mean yes, I suppose it makes sense
to you, to do something
you want to do. But that's not saying much.
all Objects you will ever create will be created with new %className%().
Unless I remove the
new operator... or you use any one of several built-in functions that create objects for you.
There is no good way to get the class object of an object in AHK.
No, not
at the moment. Built-in objects do not even have class objects, except for Object, Array and Map. Obviously, whatever you're trying to do with the class object is not going to work if there is no class object.
With Object.ahk I toyed with the idea of
aClass.Prototype.class = aClass (despite the circular reference), which would mean
(new aClass).class = aClass.
__Class should probably be
__Type, since that's what it's for; to be returned by Type. Currently the presence of
__Class is also used as a signal to prevent
__Delete from executing for prototypes, and for Type to return "Prototype", but separating this behaviour from the type name would improve flexibility.
COM objects can be identified easily. However, it's quite possible that
comobj.class returns something meaningful, perhaps even a Class object from a remote AutoHotkey script.
I suppose class objects have these purposes:
- Identify the class for obj is aClass.
- Access static methods and properties, including the constructor. Not helpful if there are none. GuiCreate, MenuCreate and MenuBarCreate can be replaced with constructors, although the functions are readily accessible even in assume-local functions. GuiFromHwnd, GuiCtrlFromHwnd and MenuFromHandle could also be replaced with static methods, but I see no real benefit.
- Provide access to Prototype without creating an instance of the object, for adding extensions.
- Be extended (be used with the extends keyword). For example, to extend functionality without risk of breaking unaware parts of the script. A subclass of GuiControl could be used to integrate a custom control with the standard Gui methods.
But classes other than Object, Array and Map were a low priority, and I was reluctant to reserve global variables named "Gui", "Menu", "File", etc. especially without first making #Warn ClassOverwrite default/mandatory.