Difficulty passing class method to function? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Difficulty passing class method to function?

24 Nov 2017, 19:57

How do you pass a class method to a function, which then calls the passed method?

Example (not-working) code:

Code: Select all

test(callback){
    ;msgbox, % Foo.bar()	;<-- works as expected
    return callback.call()	;<-- doesn't call Foo.bar?!
}

class Foo {
   bar(){
      return "Hello world"
   }
}

msgbox, % test(Foo.bar) ;<-- ""
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Difficulty passing class method to function?

24 Nov 2017, 20:09

Code: Select all

test(this, method){
        return this[method]()
}

class Foo {
   bar(){
      return "Hello world"
   }
}

msgbox % test(Foo,"bar")
You can also pass optionally parameters

Code: Select all

test(this, method, p*){
        return this[method](p*)
}

class Foo {
   bar(string:=""){
      return string?string:"No Message"
   }
}

msgbox % test(Foo,"bar","Hello World!")
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Difficulty passing class method to function?

24 Nov 2017, 20:15

HotKeyIt wrote:

Code: Select all

test(this, method){
        return this[method]()
}

class Foo {
   bar(){
      return "Hello world"
   }
}

msgbox % test(Foo,"bar")
Yes so I did actually think about that solution, but it's for a library and I want users to be able to do: Func("myAwesomeFunc") instead of Foo.bar. Could technically count the parameters and act accordingly :sick: ...
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Difficulty passing class method to function?  Topic is solved

24 Nov 2017, 20:16

Foo.bar is a function reference. You can pass it as you do, but when you call it you need to pass all required parameters, just like with any other function ref. Since, methods have a hidden parameter, this, Foo.Bar.minParams = 1.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Difficulty passing class method to function?

24 Nov 2017, 20:21

Helgef wrote:Foo.bar is a function reference. You can pass it as you do, but when you call it you need to pass all required parameters, just like with any other function ref. Since, methods have a hidden parameter, this, Foo.Bar.minParams = 1.
Oh... poop. So... How do I attach the parent object as a parameter? :wtf:
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Difficulty passing class method to function?

24 Nov 2017, 20:26

Foo.bar.bind(foo). Then you can use this in bar which can use static variables in foo and other methods.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Difficulty passing class method to function?

24 Nov 2017, 20:37

Helgef wrote:Foo.bar.bind(foo). Then you can use this in bar which can use static variables in foo and other methods.
I mean in general, I don't even need instances of the class, static variables or other methods. I just wanted an easy way to package up some functions without polluting the global scope... xD

I guess it seems that callbacks aren't really a thing in ahk, or you don't get as much flexibility than in other languages (unless I am missing something here)
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Difficulty passing class method to function?

24 Nov 2017, 21:28

Hi Sancarn,
I don't even need instances of the class, static variables or other methods.
So what could be the purpose of binding the parent object?
I just wanted an easy way to package up some functions without polluting the global scope... xD
Maybe you have in view something like this:

Code: Select all

obj := {f:Func("f"), g:Func("g"), key:0.4}
obj.f()
obj.m := Func("h")
MsgBox % obj.g()

f(this) {
MsgBox % this.key
}
g(this) {
return this.m()
}
h(this) {
return "Hello, world!"
}
hope this helps


[EDIT]

Btw you also can, use ObjBindMethod function instead:

Code: Select all

test(__callback) {
return __callback.call()
}
Class Foo {
   bar() {
   return "Hello world"
   }
}

MsgBox % test(ObjBindMethod(Foo, "bar"))
my scripts
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Difficulty passing class method to function?

25 Nov 2017, 05:43

Oh so I didn't realise you can also just pass 0 for this:

Code: Select all

test(method){
        return method.call(0)
}

class Foo {
   bar(){
      return "Hello world"
   }
}

msgbox % test(Foo.bar)
A_AhkUser: Maybe you have in view something like this
Your first example still pollutes the global scope with functions f,g and h. I just preferred to pollute it with 1 variable instead of N... As to your 2nd example, that's much more in line wit what I had in mind! Thanks for this!
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Difficulty passing class method to function?

25 Nov 2017, 07:05

Yes you can pass anything as this if you do not want to use this.
I mean in general
If you like, you can wrap your function lib in a super-class, which routes calls to the function lib, passing this when appropriate. I use the Foo-Bar notation since you started it, normally, when someone starts using more than 1 or 2 foos and bars I stop reading,

Code: Select all

test(callback){
    return callback.call()
}

class Foo {
	; "function lib"
	fooBar := "foo_bar"	; "static" variable
	class _Foo{	; the fn lib
		bar(){
			return "Hello world" "`n" this.foo()
		}
		foo(bar := 37){
			return this.fooBar "`t" bar
		}
	}
	; "Internal"
	__new(){
		Foo := this
		return ""
	}
	__call(fn, foobars*){
		static _Foo := Foo._Foo
		return _Foo[fn].call(this, foobars*)
	}
	__get(foobars*){
		static _Foo := Foo._Foo
		return _Foo[foobars*].bind(Foo)
	}
	static __Foo := new Foo
}

msgbox % test(Foo.bar)
msgbox % test(Foo.foo)
msgbox % Foo.bar()
msgbox % Foo.foo()
msgbox % Foo.fooBar
fooFn := Foo.foo
msgbox % fooFn.call(3737)
msgbox % %fooFn%(373737)
As you can see, the user of the function lib can use it as it pleases, Foo.bar(), Foo.bar.Call() or fooFn:=Foo.Bar then fooFn.call() or %fooFn%(). You can access "static" variables and other methods. There is one super-global variable, Foo. I leave it as an exercise for you to ponder how to enable use of properties.
The usefulness of the above probably ends at the understanding of it.

Cheers and good luck.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Difficulty passing class method to function?

26 Nov 2017, 09:42

Helgef wrote:Normally, when someone starts using more than 1 or 2 foos and bars I stop reading.
Yep, me too. Your code is super confusing with all the foos and bars :P But I do understand what is going on. I originally didn't understand the point of the "this" variable, but now I understand that it's only there to store functions / variables which are part of the object that that function came from (but not the function itself). So if a function in a class calls another function in that class, you have to pass "this" to that function.

So it is actually quite simple. I just didn't understand it at first.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ThePeter and 225 guests