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

Propose new features and changes
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

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

Post by RobertL » 26 Jul 2014, 00:04

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#
Last edited by RobertL on 26 Jul 2014, 23:48, edited 3 times in total.
我为人人,人人为己?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Define sub-function in function AHK V2

Post by Coco » 26 Jul 2014, 00:20

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.
User avatar
joedf
Posts: 6486
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: Define sub-function in function AHK V2

Post by joedf » 26 Jul 2014, 00:39

Cool... But I thought classes were made for that..?!?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Define sub-function in function AHK V2

Post by Coco » 26 Jul 2014, 00:49

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...
User avatar
RobertL
Posts: 540
Joined: 18 Jan 2014, 01:14
Location: China

Re: Define sub-function in function AHK V2

Post by RobertL » 26 Jul 2014, 01:14

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.
我为人人,人人为己?
User avatar
joedf
Posts: 6486
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: Define sub-function in function AHK V2

Post by joedf » 26 Jul 2014, 11:52

I see... So it's a type of style?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Define sub-function in function AHK V2

Post by Coco » 26 Jul 2014, 12:09

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.
User avatar
joedf
Posts: 6486
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: Define sub-function in function AHK V2

Post by joedf » 26 Jul 2014, 19:14

Ok, I see. Thx
lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Define sub-function in function AHK V2

Post by lexikos » 26 Jul 2014, 23:14

It's not feasible with the current code base.
lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

Post by lexikos » 01 Apr 2018, 02:13

Nested functions and closures were added in v2.0-a090-ae96c4a.
Post Reply

Return to “Wish List”