accurately determine if there is nested-functions

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

accurately determine if there is nested-functions

31 Mar 2018, 16:15

The "isfunc" now does not apply to the nested-functions. Take this code as an example:

Code: Select all

test(1)
test(this){
	t:="_" type(this)

	if isfunc(t)
		%t%()

	_string(){
	}

	_object(){
	}
}

_Integer(){

}

In this code, if you do not want to falsely trigger the "_Integer" function, you need a lot of extra code.
Then can add a function that is suitable fornested-functions? For example issubfunc() OR isfunc(,1)
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: accurately determine if there is nested-functions

31 Mar 2018, 18:04

You can do the opposite and invert the result.

Code: Select all

test("foo")
test(1)
test(this){
    t:="_" type(this)
    if isfunc(t) && !IsGlobalFunc(t)
        %t%()
    else
        MsgBox("no")	
    _string() => MsgBox("string")
    _object() => MsgBox("object")
}
_Integer() => MsgBox(":(")

IsGlobalFunc(f) => IsFunc(f)
Or you can use a more predictable data set; i.e. one you define explicitly.

Code: Select all

test("foo")
test(1)
test(this){
    fswitch := {
        string: Func("_string"),
        object: Func("_object")
    }
    if f := fswitch[type(this)]
        %f%()
    else
        MsgBox("no")
    _string() => MsgBox("string")
    _object() => MsgBox("object")
}
_Integer() => MsgBox(":(")
The second case might be more efficient. fswitch can be static if the functions are not closures. The functions could be defined inline (in fswitch) in this particular example, but Func("_string") is resolved at load time anyway.

The first case might be optimized slightly by reducing the number of times the function name must be resolved:

Code: Select all

    if (f := Func(t)) && !IsGlobalFunc(t)
        %f%()
However, searching for a function which doesn't exist is slow.

Both !IsGlobalFunc() and IsSubFunc() have a problem: what if the function is nested, but in an outer function (not the current function)? You can define an IsOuterFunc() in the outer function...

IsGlobalFunc has another issue: what if there is a nested function, but there is also a global function with the same name?

Code: Select all

test("foo")
test(1)
test(this){
    t:="_" type(this)
    if (f := Func(t)) && !IsFuncGlobal(f)
        %f%()
    else
        MsgBox("no")	
    _string() => MsgBox("string")
    _object() => MsgBox("object")
}
_Integer() => MsgBox(":(")
_string() => MsgBox(":(")

IsFuncGlobal(f) => Func(f.Name) = f
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: accurately determine if there is nested-functions

01 Apr 2018, 18:11

So I tend to modify isfunc() and add one parameter:

Code: Select all

isfunc() 	;All
isfunc(,1) 	;only Global
isfunc(,2) 	;only fornested
Maybe: How many internal functions exist for each function, there must be a list. Can we access this list?
Maybe: func("test").issub("t")

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 18 guests