New object model diagram?

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: 104
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.
JoeSchmoe
Posts: 52
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: 2255
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)
JoeSchmoe
Posts: 52
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: 7201
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 3 guests