New object model diagram?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

New object model diagram?

23 Nov 2020, 12:34

I keep having issues visualizing the new prototype-based object model, especially how class, class.prototype, object, and object.prototytpe all relate. I know we have this explanation from the documentation:
Each class object is based on whatever class it extends, or Object if not specified. The global class object Object is based on Class.Prototype, which is based on Object.Prototype, so classes can inherit methods and properties from any of these base objects.
So given all that, let's say we have the following objects:

Code: Select all

class animal {
}
class dog extends animal {
}
wally := animal.new()
fido := dog.new()
Is this how all the base and prototype relationships work?

Code: Select all

any.prototype                                     class
(type:prototype)                                  (type:class)
   /|\                                                |                        
    | (base)                                   (base) | 
    |             (base)                  (base)     \|/                            
object.prototype  <----- class.prototype  <-----  object        
(type:prototype)         (type:prototype)         (type:class)  
   /|\                                              /|\         
    | (base)                                  (base) |
    |             (base)                             |          
animal.prototype  <----- wally                    animal        
(type:prototype)         (type:animal)            (type:class)  
   /|\                                              /|\         
    | (base)                                  (base) |
    |             (base)                             |          
dog.prototype     <----- fido                     dog           
(type:prototype)         (type:dog)               (type:class)  
I'm certainly not arguing with the structure, but the base relationships: class -> object -> class.prototype -> object.prototype always trip me up (if I, in fact, even have that right). I guess maybe because it seems different in structure than the other levels. For example, object and class have their own prototypes in their base chain, whereas no user-defined class does, right? Does my diagram reflect all these relationships correctly? Is there a better way to think about this? Thanks for any thoughts you all may have.
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: New object model diagram?

28 Dec 2020, 12:51

Hey sirksel,

Did you ever make any progress on this? If so, could you post the answer here?

I'm trying to update my object model knowledge from v1 to v2 and I was thinking of pasting my findings in here. I won't if you've already figured it out, though.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: New object model diagram?

28 Dec 2020, 19:45

Hope this helps:

Code: Select all

TypeOf(obj) {
    struct:=""
	if ComObjType(obj)
		return "ComObj"
	Loop {
		struct.=obj.__Class "." Type(obj) "`n",obj:= obj.base
	} Until obj.__Class = "Any"
	return struct
}
class animal {
}
class dog extends animal {
}
wally := animal.new()
fido := dog.new()
MsgBox TypeOf(wally)
MsgBox TypeOf(fido)
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: New object model diagram?

08 Jan 2021, 13:01

Thanks so much, HotKeyIt. I'm a bit overwhelmed with other work now, but will carefully study this later.

For anyone else, what I suspect HotKeyIt has pointed out is that we can explore AHK2's object model by using the .base and .__Class properties. This will give definitive answers without needing to comb through documentation. I will post more when my schedule clears up.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: New object model diagram?

30 Jan 2021, 06:00

The diagram appears to be correct.

It might help to think of it not as one model, but multiple models utilizing (or conjoined by) a common mechanism (.base).

Object and Class have Object.Prototype in their base chain because they are both Objects (Object is Object, Class is Object), and Class.Prototype because they are both classes (Object is Class, Class is Class). By contrast, MyUserDefinedClass is a class, but is not a MyUserDefinedClass (MyUserDefinedClass is Class is true, MyUserDefinedClass is MyUserDefinedClass is false); nor does the class inherit the instance properties and methods defined within its body (i.e. the properties and methods of its Prototype).
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: New object model diagram?

25 Aug 2021, 09:58

@lexikos or others who might know... quick question: I'm updating libraries for the beta release but it seems like something may have changed since my attempt at a diagram above? I discovered that Any was inserted between Object.base and Class.Prototype in the object model. All other relationships I tested still hold true, I think:

Code: Select all

    (class.base == object)                       ;true
    (object.base == class.prototype)             ;NOW FALSE
    (object.base == any)                         ;NOW TRUE
    (any.base == class.prototype)                ;NOW TRUE
    (class.prototype.base == object.prototype)   ;true
    (object.prototype.base == any.prototype)     ;true
I was trying to find a reference to what changed (and what else may have changed) about these high level relationships in the object model, but couldn't find anything. I do know that the primitive objects were added (Integer, String, etc.), to complement (and in the same relative hierarchy as) the prototypes for them that already existed. Are there any other big changes in these high level objects? Many thanks!

EDIT: I may have found a partial answer in the changelog to a106:
Added primitive types and Any to the type hierarchy. Added prototypes for Any, Primitive, String, Number, Integer and Float; not yet accessible except through a value of the applicable type. All values are based on Any (except ComObjects) and can use HasBase, HasProp, HasMethod and GetMethod, which are now also functions. Define/Delete methods can't be called directly on the prototypes as they are not derived from Object.Prototype.
Anyhow, it appears my diagram above is now outdated... and "all values are based on Any" has more impact than I originally thought it did.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: New object model diagram?

26 Aug 2021, 02:47

class Object extends Any
Source: Object - Methods & Properties | AutoHotkey v2
Any is the class at the root of AutoHotkey's type hierarchy. All other types are a sub-type of Any.
Source: Any - Methods & Properties | AutoHotkey v2
... and being the root class, it has no superclass but is a class and therefore derives from Class.Prototype directly.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: AHKUser1234, downstairs, gsxr1300, Quixilvr, songdg and 38 guests