List all the methods from a base class? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
tic
Posts: 92
Joined: 03 Nov 2014, 03:10

List all the methods from a base class?

19 May 2017, 13:33

How can we list all the methods from a baseClass?

The following will work by explicitly using MyClass by name, but how can we use this to get the methods myMethod1 and myMethod2?

Code: Select all

myc := new MyClass()
return

class MyClass extends BaseClass
{
	myMethod1() {
	}
	
	myMethod2() {
	}
}

class BaseClass
{
	__New(init := false)
	{
		if (!init)
		{
			;The commented does not work
			;myc := new this(true)
			;for k, v in myc
			for k, v in MyClass
			{
				MsgBox, % k "`n" v
			}
		}
	}
}
tic
Posts: 92
Joined: 03 Nov 2014, 03:10

Re: List all the methods from a base class?

19 May 2017, 13:55

I had not tried putting the percentages around a. Very odd syntax, considering both k and v are objects and do not require % to qualify them.
Anyway, thanks!
Helgef
Posts: 3296
Joined: 17 Jul 2016, 01:02
Contact:

Re: List all the methods from a base class?

19 May 2017, 14:06

Indeed, see %var% (double-deref).
Cheers.
lexikos
Posts: 6207
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: List all the methods from a base class?

19 May 2017, 17:20

this.base refers to the class directly. :facepalm:
User avatar
jeeswg
Posts: 5406
Joined: 19 Dec 2016, 01:58
Location: UK

Re: List all the methods from a base class?

19 May 2017, 18:09

This is interesting, it's one of the few times I've seen __Class or __Init anywhere. Btw so if you test this on a normal AHK object, it shows nothing?

Code: Select all

q:: ;get object methods
vOutput := ""

obj := {}
vOutput .= "AHK object`r`n"
for k, v in obj.base
	vOutput .= k "`t" v "`r`n"
vOutput .= "`r`n"

obj := new MyEmptyClass
vOutput .= "MyEmptyClass`r`n"
for k, v in obj.base
	vOutput .= k "`t" v "`r`n"
vOutput .= "`r`n"

obj := new My3MethodClass
vOutput .= "My3MethodClass`r`n"
for k, v in obj.base
	vOutput .= k "`t" v "`r`n"
vOutput .= "`r`n"

MsgBox, % vOutput
obj := ""
return

class MyEmptyClass
{
}

class My3MethodClass
{
	Num := 42
	MyMethod1()
	{
	}
	MyMethod2()
	{
	}
	MyMethod3()
	{
	}
}

;==================================================

;output:
;AHK object

;MyEmptyClass
;__Class	MyEmptyClass

;My3MethodClass
;__Class	My3MethodClass
;__Init
;MyMethod1
;MyMethod2
;MyMethod3

;note: there is an __Init because there is stuff in the class body i.e. 'Num := 42'
A_AhkUser
Posts: 884
Joined: 06 Mar 2017, 16:18
Location: France

Re: List all the methods from a base class?

19 May 2017, 18:41

Naturally since you loop throught .base - compare:

Code: Select all

q:: ;get object methods
vOutput := ""

obj := {base:{__Get:Func("MyFunc")}, "b":5}
vOutput .= "AHK object`r`n"
for k, v in obj.base
	vOutput .= k "`t" v "`r`n"
vOutput .= "`r`n"

obj := new MyEmptyClass
vOutput .= "MyEmptyClass`r`n"
for k, v in obj.base
	vOutput .= k "`t" v "`r`n"
vOutput .= "`r`n"

obj := new My3MethodClass
vOutput .= "My3MethodClass`r`n"
for k, v in obj.base
	vOutput .= k "`t" v "`r`n"
vOutput .= "`r`n"

MsgBox, % vOutput
obj := ""
return

MyFunc(this) {
MsgBox % this.b
}
class MyEmptyClass
{
}

class My3MethodClass
{
	Num := 42
	MyMethod1()
	{
	}
	MyMethod2()
	{
	}
	MyMethod3()
	{
	}
}

;==================================================

;output:
;AHK object

;MyEmptyClass
;__Class	MyEmptyClass

;My3MethodClass
;__Class	My3MethodClass
;__Init
;MyMethod1
;MyMethod2
;MyMethod3

;note: there is an __Init because there is stuff in the class body i.e. 'Num := 42'

A_AhkUser
Posts: 884
Joined: 06 Mar 2017, 16:18
Location: France

Re: List all the methods from a base class?

20 May 2017, 09:58

Code: Select all

Class C {

	prop := 7
	__Init() {
	var := "test"
	}

} ; Error at line 148. Line Text: __Init Error: Duplicate declaration. The program will exit.
> Doesn't work: duplicate declaration due to the prop instance variable declaration:
documentation wrote:These declarations are evaluated each time a new instance of the class is created with the new keyword. The method name __Init is reserved for this purpose (...)

Code: Select all

Class C {

	__Init() {
	this.prop := A_NowUTC
	}
	__New() {
	MsgBox % this.prop
	}

}
K1 := new C()
sleep, 1000
K2 := new C()
>It appears that this works - however the documentation asserts that
[__init method] should not be used by the script

Code: Select all

Class C {

	static dependencies :=[ A_ScriptDir . "\settings.ini", A_ScriptDir . "\HTMLFile.html"]
	
	prop := 7
	
	Init() { ; works
	
	static __ := C.init()
	
		for __index, __file in C.dependencies
			if not (FileExist(__file)) {
				C.exception := __index
			ExitApp
			}
				
				
	}
	__New() {
	MsgBox % A_ThisFunc
	}
	exception {
		set {
		MsgBox, 16, Initialization error, The program will exit. Specifically: %value%
		}
	}

}
K := new C()
> Works since the method name doesn't start with two underlines; it is not a meta-function.

Return to “Ask For Help”

Who is online

Users browsing this forum: Hellbent and 28 guests