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%
}
Understanding BoundFunc Object Topic is solved
Re: Understanding BoundFunc Object Topic is solved
Code: Select all
add(a, b) {
MsgBox % a + b
}
boundFuncThatCalculates1Plus2 := Func("add").Bind(1, 2)
Menu MyMenu, Add, % "1 + 2 = ???", % boundFuncThatCalculates1Plus2
Menu MyMenu, Show
- u have a function defined somewhere. in this case a user-define function: add
- u retrieve a function reference to it: Func("add")
- u bind arguments to that function reference: .Bind(1, 2). that gives u a "bound function object" back
- u pass the bound func object to Menu, , Add's LabelOrSubmenu parameter
- 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
Re: Understanding BoundFunc Object
Cool function, this changes the way menu can be used .swagfag wrote: ↑23 Oct 2019, 00:21Code: Select all
add(a, b) { MsgBox % a + b } boundFuncThatCalculates1Plus2 := Func("add").Bind(1, 2) Menu MyMenu, Add, % "1 + 2 = ???", % boundFuncThatCalculates1Plus2 Menu MyMenu, Show
- u have a function defined somewhere. in this case a user-define function: add
- u retrieve a function reference to it: Func("add")
- u bind arguments to that function reference: .Bind(1, 2). that gives u a "bound function object" back
- u pass the bound func object to Menu, , Add's LabelOrSubmenu parameter
- 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
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)
Re: Understanding BoundFunc Object
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
re. ur 2nd question, no: see single variable reference
Re: Understanding BoundFunc Object
@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.
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.