[Wish:] Wish to use "For Else If !A_index{" to replace the "current For Else{"

Discuss the future of the AutoHotkey language
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

[Wish:] Wish to use "For Else If !A_index{" to replace the "current For Else{"

16 Sep 2023, 11:03

Wish to use For ...Else If !A_index{ to replace the current For ..Else{
As we know in Python, if the whole traverse is successfully finished, then the else block will run. It's different from the current Else syntax in AHKV2, but it is extremely useful in many situations.
Typically, this feature must be used to pop from the stack after the loop is normally finished each time, if you want to translate a recursion into loop with a stack.
[Code1:]

Code: Select all

Iter(eles*){
	en:=eles.__Enum(1)
	stack:=[en]
	stack.capacity:=eles.capacity**2
	arr:=[]
	while stack.Length{	
		for x in en{
			switch x.HasProp('__Enum') {
				case 0:
					arr.Push(x)
				case 1:
					stack.Push(en),en:=x.__Enum(1)
					; break
					goto breaked
			}
		}
		en:=stack.Pop()
		breaked:
		;here you can doing somethings else, like out of the else block.
	}
	return arr
}
c:=Iter(1,2,[3,4,[5,6,[7,8,9],10],11,[12,13]],14,15)
for x in c{
	OutputDebug(x)
}
image.png
image.png (49.47 KiB) Viewed 1276 times
With this feature, we can write like this avoiding using Goto.
[Code2:]

Code: Select all

Iter(eles*){
	en:=eles.__Enum(1)
	stack:=[en]
	stack.capacity:=eles.capacity**2
	arr:=[]
	while stack.Length{	
		for x in en{
			switch x.HasProp('__Enum') {
				case 0:
					arr.Push(x)
				case 1:
					stack.Push(en),en:=x.__Enum(1)
					break
			}
		}else{
		   en:=stack.Pop()
		}
		;here you can doing somethings else, like out of the else block.
	}
	return arr
}
c:=Iter(1,2,[3,4,[5,6,[7,8,9],10],11,[12,13]],14,15)
for x in c{
	OutputDebug(x)
}
Also, in most general cases, It would be convenient with the feature when you can't find a member.
[Code3:]

Code: Select all

arr:=['John','Smith','Micheara']
for x in arr{
	if x=='David'{
		MsgBox('I found David!')
	}
}else{
	MsgBox('I can not found David!')
}
Of course, this feature can totally replace the currently For Else usage. See Code4:
[Code4:]

Code: Select all

arr:=[]
for x in arr.__Enum(1){
	OutputDebug(x)
}else{
	MsgBox('There is no break or countinue(2) encountered.')
	if A_Index==0{
		MsgBox('The loop had zero iterations.')
	}
}
[Code5:]

Code: Select all

arr:=[]
for x in arr.__Enum(1){
	OutputDebug(x)
}else if !A_Index{
		MsgBox('The loop had zero iterations.')
}
As we can see, with this feature given, the current Else syntax become redundant, bacause it is just a small case of what this feature solved.
Therefore, I wish this feature could replace the current Else as soon as possible.
If you are concerning about the backward compatibility, it is absolutely worth considering introducing another keyword for this feature.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Wish:] Wish to use "For Else If !A_index{" to replace the "current For Else{"

16 Sep 2023, 19:38

V2User wrote:If you are concerning about the backward compability, it is absolutely worth considering introducing another keyword for this feature.
To me, it is not worth more than a moment's thought. When you present your proposal in a way that cannot be accepted due to incompatibility, it won't be accepted. It's up to you to present it in a way that is acceptable, or do it yourself, if you want it implemented.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [Wish:] Wish to use "For Else If !A_index{" to replace the "current For Else{"

17 Sep 2023, 01:21

lexikos wrote:
16 Sep 2023, 19:38
V2User wrote:If you are concerning about the backward compability, it is absolutely worth considering introducing another keyword for this feature.
It's up to you to present it in a way that is acceptable.
Sorry for not knowing, perhaps because Else is a feature introduced not so long ago and a similar suggestion has already been posted here, so I guess you might accept mine.
viewtopic.php?f=37&t=114516&hilit=continue
it is absolutely worth considering introducing another keyword for this feature.
But there is still no matter. Just now, I came up with a word used in Switch that might be proper. How about Default? The word will do the above except that there are zero iterations, a little unlike Python. It would be even better than the above if combined with else, because A_index is not needed in this situation any longer.

Code: Select all

arr:=[]
for x in arr.__Enum(1){
	OutputDebug(x)
}default{
     MsgBox('The loop is finished with no break')
}else{
		MsgBox('The loop had zero iterations.')
}
Last edited by V2User on 17 Sep 2023, 05:46, edited 1 time in total.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Wish:] Wish to use "For Else If !A_index{" to replace the "current For Else{"

17 Sep 2023, 03:03

I have no idea how "default" relates to a loop.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [Wish:] Wish to use "For Else If !A_index{" to replace the "current For Else{"

17 Sep 2023, 06:04

It's an auto-executing segment which belongs to a loop block or any other blocks, and is appended to the end of a loop. Just as it's in the end of the switch structure. It's like automatically pushing a callable element to the end of an enumerator for auto-executing. It will be executed by default, each time a block is ending. It will be auto-executed upon ending by default, if there aren't any "jump outs" happening in a Switch or loop block, such as Case or Break.
In my opinion, a proper enough name will be OK. What's important is that it's useful. Otherwise, it would never be mentioned.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 130 guests