Page 1 of 1

[Not feasible] Define sub-function in function AHK V2

Posted: 26 Jul 2014, 00:04
by RobertL
AHK V2 wrote:Labels defined in a function have local scope;
they are visible only inside that function and do not conflict with labels defined elsewhere
Could support define sub-functions/subfunctions (or called nested function define?) in function?
It's more convention and reuseable than "Using Subroutines Within a Function".

Except:

Code: Select all

g(){	;1
}
f(){
	g(){	;2
	}
	g()	;2
}
g()		;1
Where inside scope, the local function (2) will override the global ones (1).
The function name of a g (2), may like f.g in name of FuncObject. Although should not be called from outside.

Currently, an error occurs "Functions cannot contain functions".

Also see my post 5#.
Not feasible, see 8#

Re: Define sub-function in function AHK V2

Posted: 26 Jul 2014, 00:20
by Coco
This is useful if a function requires some helper function(s) to accomplish its task and the user doesn't want to populate the global namespace.

Re: Define sub-function in function AHK V2

Posted: 26 Jul 2014, 00:39
by joedf
Cool... But I thought classes were made for that..?!?

Re: Define sub-function in function AHK V2

Posted: 26 Jul 2014, 00:49
by Coco
The difference is that the inline/nested function can access the local/static variables of the top function while also having its own local/static vars. Just like in Javascript, Python, and others that support closures...

Re: Define sub-function in function AHK V2

Posted: 26 Jul 2014, 01:14
by RobertL
Eh.. closures/upvalue(in Lua), I didn't thought this so far.
These feature could be simulate by using an object to represent inheritance relationships.
Also could use class to implement subfunction.

But I think it's convention if integrate/support them within syntax.
It seems to involve the core design of variable/object-relation model.
Maybe local function would be fine currently.

Re: Define sub-function in function AHK V2

Posted: 26 Jul 2014, 11:52
by joedf
I see... So it's a type of style?

Re: Define sub-function in function AHK V2

Posted: 26 Jul 2014, 12:09
by Coco
Wikipedia wrote:Lexically nested function definitions are a form of information hiding and are useful for dividing procedural tasks into subtasks which are only meaningful locally. This avoids cluttering other parts of the program with functions and variables that are unrelated to those parts.

They are typically used as helper functions or as recursive functions inside another function. This has the structural benefit of organizing the code, avoids polluting the scope, and also allows functions to share state easily. As nested function can access local variables of the enclosing function, sharing of state is possible without passing parameters to the nested function or use a global variable, simplifying code.

In languages with nested functions, functions may normally also contain local constants, and types (in addition to local variables, parameters, and functions), encapsulated and hidden in the same nested manner, at any level of depth. This may further enhance the code structuring possibilities.

Re: Define sub-function in function AHK V2

Posted: 26 Jul 2014, 19:14
by joedf
Ok, I see. Thx

Re: Define sub-function in function AHK V2

Posted: 26 Jul 2014, 23:14
by lexikos
It's not feasible with the current code base.

Re: [Not feasible] Define sub-function in function AHK V2  Topic is solved

Posted: 01 Apr 2018, 02:13
by lexikos
Nested functions and closures were added in v2.0-a090-ae96c4a.