Page 1 of 1

AHK v2: question about enumerating

Posted: 10 Dec 2018, 13:20
by vvhitevvizard
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

Re: AHK v2: question about enumerating

Posted: 10 Dec 2018, 13:44
by Helgef
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.

Re: AHK v2: question about enumerating

Posted: 10 Dec 2018, 13:59
by vvhitevvizard
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

Re: AHK v2: question about enumerating

Posted: 10 Dec 2018, 16:02
by oif2003

Re: AHK v2: question about enumerating

Posted: 10 Dec 2018, 19:03
by vvhitevvizard
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