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

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

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

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
Contact:

Re: Define sub-function in function AHK V2

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: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Define sub-function in function AHK V2

26 Jul 2014, 00:39

Cool... But I thought classes were made for that..?!?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: Define sub-function in function AHK V2

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: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: Define sub-function in function AHK V2

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: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Define sub-function in function AHK V2

26 Jul 2014, 11:52

I see... So it's a type of style?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: Define sub-function in function AHK V2

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: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Define sub-function in function AHK V2

26 Jul 2014, 19:14

Ok, I see. Thx
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
lexikos
Posts: 9552
Joined: 30 Sep 2013, 04:07
Contact:

Re: Define sub-function in function AHK V2

26 Jul 2014, 23:14

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

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

01 Apr 2018, 02:13

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

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 36 guests