Page 1 of 1

List all the methods from a base class?

Posted: 19 May 2017, 13:33
by tic
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
			}
		}
	}
}

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

Posted: 19 May 2017, 13:50
by Helgef

Code: Select all

a:=this.__Class
for k, v in %a%

Re: List all the methods from a base class?

Posted: 19 May 2017, 13:55
by tic
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!

Re: List all the methods from a base class?

Posted: 19 May 2017, 14:06
by Helgef
Indeed, see %var% (double-deref).
Cheers.

Re: List all the methods from a base class?

Posted: 19 May 2017, 17:20
by lexikos
this.base refers to the class directly. :facepalm:

Re: List all the methods from a base class?

Posted: 19 May 2017, 17:50
by Helgef
That is nicer :xmas:

Re: List all the methods from a base class?

Posted: 19 May 2017, 18:09
by jeeswg
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'

Re: List all the methods from a base class?

Posted: 19 May 2017, 18:41
by A_AhkUser
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'


Re: List all the methods from a base class?

Posted: 19 May 2017, 22:37
by Helgef
Btw, init!=__Init.

Re: List all the methods from a base class?

Posted: 20 May 2017, 09:58
by A_AhkUser

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.