Understanding BoundFunc Object Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Understanding BoundFunc Object

22 Oct 2019, 23:47

I have trouble understanding the BoundFunc Object.

I try to call a BoundFunc Object with a menu but it does not work.
In my scripts I use a lot of menu's, It would be helpfull to call directly a function with parameters instead of using a label in combination with global variables.

Is it possible to call functions by a menu with the BoundFunc Object?

Does anybody has some simple examples how to use the BoundFunc Object?
Of course I have already read the help files.

;fn := Func("RealFn").Bind(2)
fn := ObjBindMethod("test", "ReadLine")

; Trying to call a function with a menu
Menu, MyMenu, Add, Item1, % Fn.Call("3")
Menu, MyMenu, Add, Item2, MenuHandler
Menu, MyMenu, Show

return ; End of script's auto-execute section.

MenuHandler:
MsgBox You selected %A_ThisMenuItem% from the menu %A_ThisMenu%.
return

RealFn(a, b) {
MsgBox %a%, %b%
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Understanding BoundFunc Object  Topic is solved

23 Oct 2019, 00:21

Code: Select all

add(a, b) {
	MsgBox % a + b
}

boundFuncThatCalculates1Plus2 := Func("add").Bind(1, 2)

Menu MyMenu, Add, % "1 + 2 = ???", % boundFuncThatCalculates1Plus2
Menu MyMenu, Show
  1. u have a function defined somewhere. in this case a user-define function: add
  2. u retrieve a function reference to it: Func("add")
  3. u bind arguments to that function reference: .Bind(1, 2). that gives u a "bound function object" back
  4. u pass the bound func object to Menu, , Add's LabelOrSubmenu parameter
  5. now whenever u click the menu item, the function is run. additionally, ItemName, ItemPos, MenuName are passed in last as arguments by the menu handler itself, if the function defines them. in ur case it does not, so they are discarded
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Understanding BoundFunc Object

23 Oct 2019, 05:38

swagfag wrote:
23 Oct 2019, 00:21

Code: Select all

add(a, b) {
	MsgBox % a + b
}

boundFuncThatCalculates1Plus2 := Func("add").Bind(1, 2)

Menu MyMenu, Add, % "1 + 2 = ???", % boundFuncThatCalculates1Plus2
Menu MyMenu, Show
  1. u have a function defined somewhere. in this case a user-define function: add
  2. u retrieve a function reference to it: Func("add")
  3. u bind arguments to that function reference: .Bind(1, 2). that gives u a "bound function object" back
  4. u pass the bound func object to Menu, , Add's LabelOrSubmenu parameter
  5. now whenever u click the menu item, the function is run. additionally, ItemName, ItemPos, MenuName are passed in last as arguments by the menu handler itself, if the function defines them. in ur case it does not, so they are discarded
Cool function, this changes the way menu can be used :bravo: .
This can shorten a lot of code that I wrote.

This should be added in the examples of the Menu, Add documentation.

Here is an example where the parameters of menu are used:
Remarque: if you update the variables used in the BoundFunc object, the old value will be used in the BoundFunc object.

BoundGivePar := Func("GivePar").Bind("test", "test2")

Menu MyMenu, Add, Give parameters, % BoundGivePar
Menu MyMenu, Show

GivePar(a, b, ItemName, MenuName, ItemPos) {
MsgBox, a:`t`t%a%`nb:`t`t%b%`nItemName:`t%ItemName%`nMenuName:`t%MenuName%`nItemPos:`t`t%ItemPos%
}


A new question rises:
Is it possible to write this in one line?
Something similar to:
Menu MyMenu, Add, Give parameters, % Func("GivePar").Bind("test", "test2") (not working)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Understanding BoundFunc Object

23 Oct 2019, 06:01

plain variables and other literals are passed(bound, even if had used ByRef) by value, arrays/objects - by reference(so u can mutate those if u want and the changes will be visible outside the function, though ure better off just not doing this at all)

re. ur 2nd question, no: see single variable reference
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Understanding BoundFunc Object

23 Oct 2019, 15:55

@swagfag: Thanks a lot.

Below an example of calling a function (if no label with the same name exists).
Interesting to know, but because you cannot pass variables, It acts the same as a label.

Menu MyMenu, Add, Give parameters, GivePar
Menu MyMenu, Show

GivePar(ItemName, MenuName, ItemPos) {
MsgBox, ItemName:`t%ItemName%`nMenuName:`t%MenuName%`nItemPos:`t`t%ItemPos%
}


I posted a nice use case of the use of BoundFunc Objects in menus here.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, Google [Bot], Nerafius, RandomBoy and 189 guests