How to call nested function? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

How to call nested function?

11 Apr 2024, 21:09

Code: Select all

fn1()
    {
        fn2()
        {

        }
    }
niCode
Posts: 296
Joined: 17 Oct 2022, 22:09

Re: How to call nested function?

11 Apr 2024, 22:15

Normally the point of nested functions is to encapsulate it so it is scoped to only the "parent" function. It only seems logical to just take the functions out so they can be globally seen.

But if you still insist on knowing how it can be done, one way to do this is dereferencing.

Code: Select all

fn1(function) {
    %function%()

    fn2() {
        MsgBox('first nested function called')
    }

    fn3() {
        MsgBox('second nested function called')
    }
}

fn1('fn2')
fn1('fn3')
lexikos
Posts: 9593
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to call nested function?

11 Apr 2024, 23:32

The simplest answer to "How to call nested function?" is literally fn2() within f1.

What niCode demonstrated is not something I would ever suggest doing in a real script.

A more typical use of a nested function would be to return it, as in the first closure example. The caller can then assign the return value to a variable or whatever, and call it via the variable. There are any number of other ways that a value can be passed from one part of the script to another. Just treat the nested function as a value, and do whatever needs to be done to get it where you want it.

If these are not the answers you are looking for, ask a more specific question.
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: How to call nested function?

12 Apr 2024, 01:54

Thanks! Yes I think my use case is such where I build the gui with a function

Code: Select all

BuildGui()
Then I added a button and have a nested function

Code: Select all

BtnClicked(*)
Now somewhere else in the code I want to run the BtnClicked function, without having to Focus the Control and Send {enter} as the Gui Window may not be active.

Perhaps I need to use SendMessage?

But not familiar with SendMessage.

say the btn is in

Code: Select all

app:=Gui()
app.Add("Button","xm y+0" " w100" " vBtn1","Make Change")"
we could use the Hwnd with

Code: Select all

app["Btn1"].Hwnd
lexikos
Posts: 9593
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to call nested function?  Topic is solved

12 Apr 2024, 02:39

@jsong55 Do you want us to guess at the structure of your script, like we had to guess at what you were trying to do?

Don't use SendMessage to call your own function. If you want your function to be callable from outside the function, either just don't put it inside the function, or pass it out somehow (e.g. attach it to the Gui as a method).
niCode wrote:
11 Apr 2024, 22:15
Normally the point of nested functions is to encapsulate it so it is scoped to only the "parent" function. It only seems logical to just take the functions out so they can be globally seen.
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: How to call nested function?

12 Apr 2024, 03:24

Oh Brilliant suggestion. I did just that. Used BoundFunc to attach it to the Gui object as a method.
So now it can be called anywhere.
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: How to call nested function?

12 Apr 2024, 03:25

@lexikos
Sorry to make you guess. I'll use example code next time I ask the question.
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: How to call nested function?

13 Apr 2024, 01:36

jsong55 wrote:
12 Apr 2024, 03:24
Used BoundFunc to attach it to the Gui object as a method.
I am not sure how you did that.

Here's my implementation using the DefineProp method:

Code: Select all

MyGui := BuildGui()
MyGui.Show()

F3::MyGui.Click()

BuildGui() {
   MyGui := Gui()
   Button := MyGui.Add('Button', , 'OK')
   Button.OnEvent('Click', Button_Click)
   MyGui.DefineProp('Click', {Call: (this) => Button_Click()})
   return MyGui
   Button_Click(*) {
      MsgBox A_ThisFunc
   }
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
User avatar
rommmcek
Posts: 1478
Joined: 15 Aug 2014, 15:18

Re: How to call nested function?

13 Apr 2024, 16:15

iPhilip wrote:
13 Apr 2024, 01:36
I am not sure how you did that.
Replacing MyGui.DefineProp('Click', {Call: (this) => Button_Click()}) in your script with ObjBindMethod(myGui, 'Click') will do it.
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: How to call nested function?

13 Apr 2024, 18:41

rommmcek wrote:
13 Apr 2024, 16:15
Replacing MyGui.DefineProp('Click', {Call: (this) => Button_Click()}) in your script with ObjBindMethod(myGui, 'Click') will do it.
That didn't work for me. The closest thing was to replace MyGui.DefineProp('Click', {Call: (this) => Button_Click()}) with MyGui.Click := Button_Click.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: How to call nested function?

13 Apr 2024, 19:26

Code: Select all

MyGui.Click := Button_Click
This works. I used this too.
User avatar
rommmcek
Posts: 1478
Joined: 15 Aug 2014, 15:18

Re: How to call nested function?

14 Apr 2024, 05:38

Interesting! However, for me MyGui.Click := Button_Click does not work.
Try then myGui.Button:= ObjBindMethod(myGui, 'Click').
Using AutoHotkey 2.0.3 Unicode 64-bit on Win10.
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: How to call nested function?

14 Apr 2024, 06:27

@rommmcek
try

Code: Select all

MyGui.Click:=Button_Click.Bind()
User avatar
xMaxrayx
Posts: 168
Joined: 06 Dec 2022, 02:56
Contact:

Re: How to call nested function?

15 Apr 2024, 04:22

jsong55 wrote:
11 Apr 2024, 21:09

Code: Select all

fn1()
    {
        fn2()
        {

        }
    }
like this

Code: Select all

fn1(mode := 1)
{
	if mode == 2{
	fn2()
	}
}
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
jsong55
Posts: 253
Joined: 30 Mar 2021, 22:02

Re: How to call nested function?

15 Apr 2024, 05:45

@xMaxrayx thanks for your contribution but the context of my question wasn't clear. And in that context lexikos answer is best and I have marked his :)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: mikeyww and 69 guests