nested-functions ByRef parameters

Discuss the future of the AutoHotkey language
arcticir
Posts: 694
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

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

Code: Select all

test(1)

test(byref a){
	t()
	t(){
		MsgBox a
	}
}
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

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: 694
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
        ,"integer":1,"link":1,"list":1,"new":1,"object":1,"postlink":1,"run":1,"self":1,"set":1,"string":1,"sub":1,send:1
        ,"to":1,"write":1,alias:1,alias_:1,array:1,__:1,onexit:1,"start":1,Pipe:1,net:1,pivot:1,task:1,tasks:1,tls:1,_get:1
        ,post:1,_set:1,dll:1,label:1,postfunc:1,timer:1,threadFunc:1}
    type:=type(last:=this)
    if list[type]
    {
        Gosub("_" type)
        While to and list[to]
            Gosub(to)
    }
    return out
    _string:
        to:="get"
        return
    get:
        to:=""
        out:="xxx"
        return
}
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: nested-functions ByRef parameters

21 Apr 2018, 16:22

Is this a bug?

Code: Select all

MsgBox f()

f()
{
    local a := ""
    nf()
    return a
    nf()
    {
        a := "aaaaaaaaaaaaaaa"    ; if you remove an "a", it works correctly
    }
}

Edit* It seems that it has already been fixed.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 26 guests