[a129] Construction of nested class Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

[a129] Construction of nested class

Post by hoppfrosch » 23 Mar 2021, 02:54

Hi all,

this used to work with pre-a129 ahkV2 versions:

Code: Select all

class a{ 
	class b{ 
		x := 42
		y := 17
		test() {
			return 55
		}
	} 
}
c := a.new()  
d := a.b.new()
I tried the following port to a129 (simply omitting new):

Code: Select all

...
c := a()    ; WORKS
d := a.b() ; FAILS (Error: Too many parameters passed to function)
What does this error mean here? Whats the correct way to instantiate a nested class in this example?

TIA
Hoppfrosch
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: [a129] Construction of nested class  Topic is solved

Post by lexikos » 23 Mar 2021, 03:12

Nested Classes

Nested class definitions allow a class object to be associated with a static/class variable of the outer class instead of a separate global variable. In the example above, class NestedClass constructs a Class object and stores it in ClassName.NestedClass. Subclasses could inherit NestedClass or override it with their own nested class (in which case (WhichClass.NestedClass)() could be used to instantiate whichever class is appropriate).

Code: Select all

class NestedClass
{
    ...
}
Nesting a class does not imply any particular relationship to the outer class. The nested class is not instantiated automatically, nor do instances of the nested class have any connection with an instance of the outer class, unless the script explicitly makes that connection.

However, due to the way methods work for Objects, WhichClass.NestedClass() implicitly passes WhichClass as the first parameter, equivalent to WhichClass.NestedClass.Call(WhichClass). Unless static Call() is overridden, this parameter is automatically passed to __New.

Source: Objects - Definition & Usage | AutoHotkey v2
I considered adding a workaround for the base implementation of ClassObj.Call to automatically ignore the first parameter if the classname includes ".", but it seemed like that could backfire. Likewise for automatically defining Call for nested classes but still inheriting it for other classes, or implicitly defining Call for all classes even though it could be explicitly defined in a superclass.
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: [a129] Construction of nested class

Post by hoppfrosch » 23 Mar 2021, 03:29

Ah - thanks. Missing brackets ....

Have to read the documentation more concentrated - but due to the many (breaking) changes in V2 development its difficult to stay uptodate - but I'll do my best
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: [a129] Construction of nested class

Post by lexikos » 23 Mar 2021, 04:03

You can also use ClassName.Call().

Changes between alpha builds should always be noted in the updates topic. Reading the changes there is probably easier than picking out the changes in the documentation, which mostly just covers current v2 behaviour, and v2-changes, which just covers differences between v1 and the current v2 alpha.

I added a paragraph to my previous post a few minutes after posting, before realizing you were probably still online and might already be viewing my reply.
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: [a129] Construction of nested class

Post by iPhilip » 23 Mar 2021, 21:41

In case this helps anyone, I modified the above example to work as follows:

Code: Select all

class a{ 
   static x := 1
   y := 2
   class b{
      x := 42
      y := 17
      __New(WhichClass){
         MsgBox WhichClass.x
      }
      test() {
         return 55
      }
   }
}
c := a()    ; Creates an instance of class a
MsgBox c.y  ; Outputs the instance variable y of class a: 2
d := a.b()  ; Creates an instance of class b which outputs the static variable x of class a: 1
MsgBox d.y  ; Outputs the instance variable y of class b: 17
MsgBox d.test()  ; Ouputs the return value of the method test of class b: 55
a.b.Call(a) ; Calls the __New() method of class b which outputs the static variable x of class a: 1
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Post Reply

Return to “Ask for Help (v2)”