nested-functions ByRef parameters

Discuss the future of the AutoHotkey language
Posts: 647
Joined: 17 Nov 2013, 11:32

nested-functions ByRef parameters

31 Mar 2018, 17:04

I did not see restrictions on ByRef parameters in the "nested-functions" document.
Is this a normal mistake?

Code: Select all

Error:  ByRef parameters cannot be upvalues.

Specifically: a

	003: test()
	004: {
	005: t()
--->	006: {
	007: MsgBox(a)
	008: }
	010: }
	011: Exit

Code: Select all


test(byref a){
		MsgBox a
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: nested-functions ByRef parameters

31 Mar 2018, 20:33

You didn't look hard enough.
By default, a nested function may access non-dynamic local and static variables of any function which encloses it, but not ByRef parameters or variables which are created dynamically.
Source: Nested Functions
In AutoHotkey, all non-dynamic variable references are resolved at the moment the script launches, including local variables. Each function has one set of local variables which exist from the moment the script launches until it exits. The code within the function contains pointers to these variables. Recursion is handled by backing up the local variables when a second layer of the function begins and restoring them after it returns, so at any one time, the local variables belong to the top layer of the function.

Closures may be associated with any layer of a function still running, or a set of variables from a function which has since returned. So obviously, they can't refer to the "top-layer" local variables described above. What they need are "free variables", which are not tied to the top layer of the function, are not freed when the function returns, and are able to exist for as long as a closure refers to them.

Closures are implemented by detecting which variables need to be "free variables", allocating those when the outer function begins, and turning the top-layer variables in the outer and inner function into aliases of the free variables. ByRef parameters, on the other hand, accept a reference to a variable which may or may not be "free". It is impossible to tell (with certainty) at load time whether a variable is going to be passed to a ByRef parameter, since functions can be called dynamically or via objects. At the time the closure is created, it is impossible to tell which function the variable came from, and too late to convert it into a free variable.

The only feasible solution within the current framework would be to make all local variables "free", but this would reduce performance for the majority of scripts. ByRef parameters could be allowed within a limited context (when the nested function is called directly, not via a closure), but that requires either greater complexity to detect when it is used incorrectly, or allowing incorrect usages which behave counter-intuitively (the parameter behaving as non-ByRef or referring to the wrong variable).
Posts: 647
Joined: 17 Nov 2013, 11:32

Re: nested-functions ByRef parameters

01 Apr 2018, 18:19

I am sorry for my carelessness. Thank you very much for your patience. :)

My script is used to exchange data between processes. It makes heavy use of subtags within functions. Now the nested function seems to be better able to implement its functionality. In addition to the restrictions from ByRef parameters.

Code: Select all

link(this:="__",byref label:="",byref key:="",byref value:="",param*){
    static list:={"add":1,"call":1,"dyna":1,"dynatoken":1,"func":1,"funclink":1,"funclist":1,"get":1,"go":1
    if list[type]
        Gosub("_" type)
        While to and list[to]
    return out
User avatar
Posts: 1193
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina

Re: nested-functions ByRef parameters

21 Apr 2018, 16:22

Is this a bug?

Code: Select all

MsgBox f()

    local a := ""
    return a
        a := "aaaaaaaaaaaaaaa"    ; if you remove an "a", it works correctly

Edit* It seems that it has already been fixed.
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 & WIN_7+ Argentina

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 1 guest