var scope problem of fat arrow syntax in class method Topic is solved

Discuss the future of the AutoHotkey language
User avatar
aseiot
Posts: 33
Joined: 05 Mar 2017, 04:25

var scope problem of fat arrow syntax in class method

10 Sep 2019, 09:25

Document wrote:Fat arrow function is assume-local if it is nested inside another function, otherwise it is assume-global.
Document also wrote:Fat arrow syntax can be used to define a single-line method which returns an expression.
Following code show that Fat arrow syntax in class is assume-global[as documented], but it really lose some practicability. Could that be changed? Thanks

Code: Select all

class x
{
	static Func() => y := 100			; this make y to a global var
	static Func2(){
		return y := 200
	}
}


MsgBox x.func()		; 100
MsgBox x.func2()	; 200
MsgBox y			; 100
User avatar
aseiot
Posts: 33
Joined: 05 Mar 2017, 04:25

Re: var scope problem of fat arrow syntax in class method

10 Sep 2019, 18:14

Didn't notice that before. Thanks!
lexikos
Posts: 6668
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: var scope problem of fat arrow syntax in class method

11 Sep 2019, 05:52

With which version of AutoHotkey did you test this code?

With v2.0-a104, the second MsgBox is blank due to a bug. This is fixed by commit 1080a00, which is not part of any release. Did you compile it yourself?
With v2.0-a103 and earlier, a load time error is shown because static cannot be applied to methods.
With v2.0-a101 and earlier, the fat arrow syntax is not applicable to methods or top-level functions.

The topic kczx3 refers to was initially about fat arrow functions within an expression which can already contain global variable references. The last post (as of now) precedes the addition of fat arrow methods (and non-expression fat arrow function definitions).

The primary purpose of a fat arrow function is to allow the definition of a function within a function call, such as for passing a callback to SetTimer or similar. In this context, it is usually desirable for the function to be able to refer to variables from the same scope as the expression it is written within. If the expression is not inside a function, that is global scope.

Fat arrow methods have only the purpose of saving space. They are already set apart from functions as they have a hidden this parameter, and are attached to instances or the class itself (always with a name). Fat arrow methods cannot be defined within (and are not) expressions.

There probably is more value in fat arrow methods being assume-local than assume-global.

Regardless, the y in Func and in Func2 cannot refer to the same variable unless it is declared super-global. Func and Func2 do not share any scope other than global; they share context only through the hidden this parameter (and in a very limited way, base when used for super-class calls).
User avatar
aseiot
Posts: 33
Joined: 05 Mar 2017, 04:25

Re: var scope problem of fat arrow syntax in class method

11 Sep 2019, 09:44

Thanks for your reply.
lexikos wrote:
11 Sep 2019, 05:52
With which version of AutoHotkey did you test this code?

With v2.0-a104, the second MsgBox is blank due to a bug. This is fixed by commit 1080a00, which is not part of any release. Did you compile it yourself?
Yes, I compiled from the latest git head[2c4092c]
There probably is more value in fat arrow methods being assume-local than assume-global.
Sounds great. :dance:

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 8 guests