"For" is not working properly in AHK 2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

"For" is not working properly in AHK 2

17 May 2020, 21:58

In new AutoHotkey 2, I'm not able to use "for" :| Look at this code:

Code: Select all

aaa := {
	a: 1,
	b: 2
}

for k, v in aaa
{
	MsgBox k
}
This throws an error! It says, "Type mismatch".
Error: Type mismatch.
Specifically: __Enum

Line#
001: aaa := { a: 1, b: 2 }
006: For k,v in aaa
007: {
008: MsgBox(k)
009: }
010: Exit
011: Exit
011: Exit
So in AHK 2, such thing as { a: 1, b: 2 } is no longer an enumerator? I really can't figure out what's causing the error here.
Last edited by madsounds on 18 May 2020, 02:33, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: "For" is not working properly in AHK 2  Topic is solved

17 May 2020, 23:45

Code: Select all

aaa := Map(
	'a', 1,
	'b', 2
)

for k, v in aaa
	MsgBox k
or(probably not):

Code: Select all

aaa := {
	a: 1,
	b: 2
}

for k, v in aaa.OwnProps()
	MsgBox k
or(most definitely not):

Code: Select all

Object.Prototype.DefineMethod('__Enum', (this, NumberOfVars) => this.OwnProps())

aaa := {
	a: 1,
	b: 2
}

for k, v in aaa
	MsgBox k
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: "For" is not working properly in AHK 2

21 May 2020, 04:19

Thank you man! Number 3 is the best one. I know that this solution is kinda dirty, but anything is better than writing Map( aa, bb, cc, dd ) instead of old good { aa: bb, cc: dd }.

But still I don't understand how do work parameters in that line:

Code: Select all

Object.Prototype.DefineMethod( "__Enum", (this, NumberOfVars) => this.OwnProps() )
By the way, I know the line is equivalent to the code below :roll:

Code: Select all

Object.Prototype.DefineMethod( "__Enum", Func("ObjectEnum") )

ObjectEnum( this, NumberOfVars )
{
	return this.OwnProps()
}
But why should we pass this, when it says here https lexikos.github.io /v2/docs/Objects.htm#__Enum that __Enum method has syntax __Enum( NumberOfVars )? Is it truly necessary to pass this?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: "For" is not working properly in AHK 2

21 May 2020, 04:31

the first parameter of all methods has to be this
when u write a class the normal way u dont have to include it because ahk automatically does it for u, but its there nonetheless
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: "For" is not working properly in AHK 2

21 May 2020, 06:28

Oh I've just discovered that this is a feature of DefineMethod described here: https lexikos.github.io /v2/docs/objects/Object.htm#DefineMethod
The function must accept at least one parameter, which receives a reference to the target object of the method call. This parameter is defined automatically with the name this when the method is created by a class definition.
Thanks again!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken, vmech and 63 guests