How to distinguish between many types of object?

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

How to distinguish between many types of object?

16 Feb 2020, 08:16

Type () can be used to distinguish obj-map_arr, but I don't know how to classify some special objects, such as class. Does the class object have any characteristics that can be used to judge?

for i,n in type(t)="object"?t.OwnProps():t

The current solution is to create a list of all possible occurrence types, and if it is not in the list, it is a class.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to distinguish between many types of object?

16 Feb 2020, 08:58

Code: Select all

Class test{
}
MsgBox Type(test) ;Class
MsgBox test.prototype.__Class ;test
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: How to distinguish between many types of object?

16 Feb 2020, 09:12

such?

Code: Select all

Class test{
}
t:=test.new()
MsgBox Type(t) ;Class
MsgBox t.prototype.__Class ;test
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to distinguish between many types of object?

16 Feb 2020, 11:34

Code: Select all

Class test{
}
t:=test.new()
MsgBox t.base.base.__Class ;Object
MsgBox Type(t) ;test
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: How to distinguish between many types of object?

16 Feb 2020, 12:20

Code: Select all

MsgBox [].base.base.__Class ;Object
So you have no characteristics.

In fact, there is only one place I need to judge its exact type:
for k,v in obj ;--->> obj? or obj.OwnProps()?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to distinguish between many types of object?

16 Feb 2020, 13:43

Probably:

Code: Select all

Class test{
}
t:=test.new()
t.1:=1
t.a:=2
for k, v in (t.hasMethod("__Enum")?t:t.OwnProps())
  MsgBox k "`n" v
EDIT: fixed code
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to distinguish between many types of object?

16 Feb 2020, 15:37

Actualy it is easier to add __Enum to object class:

Code: Select all

Class test{
  __Enum(*) => this.OwnProps()
}
t:=test.new()
t.1:=1
t.a:=2
for k, v in t
    MsgBox k "`n" v
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: How to distinguish between many types of object?

16 Feb 2020, 15:49

Thank you. We should consider that the source of many classes is unknown.
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: How to distinguish between many types of object?

18 Feb 2020, 10:15

Struct does not have hasMethod, hasMethod("__Enum ") does not seem to be the perfect solution.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to distinguish between many types of object?

18 Feb 2020, 16:53

I have added HasMethod to Struct.
Note that not all objects (COM) will support __Enum/for loop.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to distinguish between many types of object?

20 Feb 2020, 12:43

Hi :wave:.

You might find :arrow: is useful.

Cheers.

Edit, you are correct :thumbup:.
Last edited by Helgef on 07 Mar 2020, 13:00, edited 1 time in total.
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: How to distinguish between many types of object?

21 Feb 2020, 05:22

It does not help judge the two:
obj.__Enum or obj.OwnProps()

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 24 guests