[v2 beta1] calling function from variable value as reference Topic is solved

Report problems with documented functionality
RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

[v2 beta1] calling function from variable value as reference

Post by RomAnT » 27 Sep 2021, 06:07

This works when defied in main code

Code: Select all

mess() {
	msgbox "Hello World!"
}

outFunc() {
	inFunc(myStr) {
		%myStr%()
	}
	
	myVar := "mess"
	inFunc(myVar)
}
When definition made inside shortcut same code gives error

Code: Select all

^R:: {
	mess() {
		msgbox "Hello World!"
	}

	outFunc() {
		inFunc(myStr) {
			%myStr%()   ; <--- Error:  Variable not found. Specifically: mess
		}
		
		myVar := "mess"
		inFunc(myVar)
	}
}

User avatar
thqby
Posts: 396
Joined: 16 Apr 2021, 11:18
Contact:

Re: [v2 beta1] calling function from variable value as reference

Post by thqby » 06 Oct 2021, 04:36

RomAnT wrote:
27 Sep 2021, 06:07
This works when defied in main code

Code: Select all

mess() {
	msgbox "Hello World!"
}

outFunc() {
	inFunc(myStr) {
		%myStr%()
	}
	
	myVar := "mess"
	inFunc(myVar)
}
When definition made inside shortcut same code gives error

Code: Select all

^R:: {
	mess() {
		msgbox "Hello World!"
	}

	outFunc() {
		inFunc(myStr) {
			%myStr%()   ; <--- Error:  Variable not found. Specifically: mess
		}
		
		myVar := "mess"
		inFunc(myVar)
	}
}
example 1, %"mess"% be resolved to mess (global var).
example 2, inFunc is not a closure function, so %"mess"% be resolved to mess (global var) too.

Code: Select all

^R:: {
	mess() {
		msgbox "Hello World!"
	}

	outFunc() {
		inFunc(myStr) {
			(mess)
			%myStr%()   ; <--- Error:  Variable not found. Specifically: mess
		}
		
		myVar := "mess"
		inFunc(myVar)
	}
	outFunc()
}
example 3, inFunc is a closure function, and local vars has var that named "mess", so %"mess"% will be resolved to mess (local var).

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: [v2 beta1] calling function from variable value as reference

Post by TheArkive » 07 Oct 2021, 01:06

Also, in several cases, you can just call the func var directly:

Code: Select all

myFunc() {
    return "test"
}

var := myFunc

msgbox var() ; returns "test" in a msgbox
%var% is now a "double deref", so it's looking for another "level" of a referenced value that isn't there.

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: [v2 beta1] calling function from variable value as reference  Topic is solved

Post by lexikos » 02 Oct 2022, 03:36

The code in the first post never gave any errors, because outFunc isn't actually called. Adding a call to outFunc() does trigger the error with v2.0-beta.1.

mess is not a Closure, because it does not capture any variables (or even refer to any variables). Adding MsgBox Type(mess) confirms this; it shows "Func".

If any non-dynamic reference to mess is added in outFunc or inFunc, the dynamic reference works. This isn't supposed to be necessary for static variables - it was a bug.

It was fixed at some point between v2.0-beta.1 and v2.0-beta.11.

Post Reply

Return to “Bug Reports”