[V2.0.11:] An inconsistence in "static method" Topic is solved

Report problems with documented functionality
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

[V2.0.11:] An inconsistence in "static method"

08 Feb 2024, 01:22

I think CCC.CC2() should act like a static method in Code1 and pass CCC as ths, but it throws instead.
[Code1:]

Code: Select all

class CCC {
	class CC2 {
		static Call(ths){

		}
	}
}
c:=CCC.CC2()
image.png
image.png (26.9 KiB) Viewed 387 times
Marium0505
Posts: 40
Joined: 11 May 2020, 20:45

Re: [V2.0.11:] An inconsistence in "static method"

08 Feb 2024, 03:38

Quote from

https://www.autohotkey.com/docs/v2/Objects.htm#Custom_Classes_class
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.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [V2.0.11:] An inconsistence in "static method"

08 Feb 2024, 07:13

@Marium0505
I have seen it you gave.
However, if the nested class CCC.CC2 is the member of an object rather than the member of an class, the same code will work instead:
[Code2:]

Code: Select all

class CCC {
	class CC2 {
		static Call(ths){

		}
	}
}
; c:=CCC.CC2()
CCC2:={}
CCC2.CC2:=CCC.CC2
c2:=CCC2.cc2()
;both of them will output 1:
OutputDebug(CCC is Object),OutputDebug(CCC2 is Object)
;it's inconsistent too:
CCC.DefineProp('CC3',{call:CCC.CC2})
c3:=CCC.CC3()
image.png
image.png (32.37 KiB) Viewed 347 times
Obviously, they are not consistent. Because CCC as well as CCC2 is an object in fact. So both of the objects should behave the same.
Therefore, I think the current behavior of CCC should be modified to keep consistent with CCC2.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [V2.0.11:] An inconsistence in "static method"

15 Feb 2024, 23:08

The quoted paragraph applied to pre-release versions; it is not correct for v2.0.0+, as is demonstrated by the following:

Code: Select all

#Requires AutoHotkey v2

class WhichClass {
	class NestedClass {
		__new(x,*) {
			MsgBox type(x)
		}
	}
}

WhichClass.NestedClass(1) ; Integer, not Class
The behaviour is currently only explained in v2-changes:
Nested class definitions now produce a dynamic property with get and call accessor functions instead of a simple value property. This is to support the following behaviour:
  • Nested.Class() does not pass Nested to Nested.Class.Call and ultimately __New, which would otherwise happen because this is the normal behaviour for function objects called as methods (which is how the nested class is being used here).
  • Nested.Class := 1 is an error by default (the property is read-only).
  • Referring to or calling the class for the first time causes it to be initialized.
Source: Changes from v1.1 to v2.0 | AutoHotkey v2
I think there is no particular reason for a nested class to behave identically to a static method. It is a class definition, not a method definition. If you want to make it act differently, you can do whatever you want with DefineProp during class initialization.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [V2.0.11:] An inconsistence in "static method"

18 Feb 2024, 03:15

Marium0505 wrote:
08 Feb 2024, 03:38
Quote from

https://www.autohotkey.com/docs/v2/Objects.htm#Custom_Classes_class
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.
Sorry, maybe I have some misunderstanding of the paragraph you quoted.🤣 It supports my point, rather than being against.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [V2.0.11:] An inconsistence in "static method"  Topic is solved

22 Mar 2024, 20:08

The documentation error has been fixed in v2.0.12.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 60 guests