nested-functions ByRef parameters

Discuss the future of the AutoHotkey language
arcticir
Posts: 494
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] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Download] GeSHi © Codebox Plus

test(1)

test(byref a){
t()
t(){
MsgBox a
}
}
lexikos
Posts: 5896
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).
arcticir
Posts: 494
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] [Expand] [Download] GeSHi © Codebox Plus

User avatar
Flipeador
Posts: 655
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: nested-functions ByRef parameters

21 Apr 2018, 16:22

Is this a bug?

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus




Edit* It seems that it has already been fixed.
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 3 guests