AHK v2: question about enumerating

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

AHK v2: question about enumerating

10 Dec 2018, 13:20

1.
R there new ways in AHK v2 to distinguish between [] and {} types of user-created objects?

Code: Select all

a:=0
for i in object ;throws exc here if its not an object or doesn't have _NewEnum() method.
	if(!(a:=i=A_Index)) ;a=0:associative (have pairs "key":value) or sparse array ([1,,3])
		break
so knowing which array type the object is, I would serialize it (convert to json-like exportable string)
as a simple non-associative array
["word1","word2", "word3"]
or as pairs "key":value
{"1":"word1","2":"word2","4":"word3"}
depending on the type.
But browsing thru every object's element for that seems suboptimal. Type(object) says just "Object" for both {} and [] created arrays.

2.
And second question is there a way to distinguish between built-in objects like File, BoundFunc, etc and user-created ones which return a class name with Type(object)?
object.HasKey(__Class) or object.__Class or object.IsBuiltin throws an exception "Unknown property" when accessing built-in objects
Last edited by vvhitevvizard on 10 Dec 2018, 19:18, edited 5 times in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: AHK v2: question about enumerating

10 Dec 2018, 13:44

1.
Type(object) says just "Object" for both {} and [] created arrays.
Because there is curretnly no difference.

2.
You can check if an object is of a paricular (built-in) type by doing type(obj) == 'TypeName', but there is no such thing as a IsBuiltInObject() function though. I do not think it is useful.

Cheers.
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: AHK v2: question about enumerating

10 Dec 2018, 13:59

Helgef wrote:
10 Dec 2018, 13:44
Because there is curretnly no difference.
thank u for the quick reply!

yeah, I've seen it in the documentation. I, for one, hate ambiguity in programming languages and can't wait for the moment Lexicos decides these two array types become different. He already removed <> operator (as duplicate for !=) and we seem to don't care about backward compatibility, so why not to pay attention to this matter. :D
You can check if an object is of a particular (built-in) type by doing type(obj) == 'TypeName', but there is no such thing as a IsBuiltInObject() function though. I do not think it is useful.
The problem is there r tons of built-in object types like "Gui", "GuiDDL", "GuiEdit", "GuiCheckBox", u name it...

Use-case here is for inner objects' keys/sub-objects to be output in form of json-like formats and for debugging.
I'm looking for a way to loop-for enumerate arrays, objects and user-created prototype classes/instanced objects while skipping built-in objects like Gui, Menu, File, etc
User avatar
vvhitevvizard
Posts: 454
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: AHK v2: question about enumerating

10 Dec 2018, 19:03

well, as for now I ended up with the following:

Code: Select all

try
	a:=o.__Arr ;trying to get a unique key
catch{ ;"Unknown property" exception means its a built-in inenumerable object
	t:=Type(o)
t = Object type instead of object content to pretty-print my instanced classes and other global vars like this:
Image

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, Draken, JPMuir, kunkel321 and 54 guests