[Wish:] Wish to use "For Else If !A_index{" to replace the "current For Else{"
Posted: 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:]
With this feature, we can write like this avoiding using Goto.
[Code2:]
Also, in most general cases, It would be convenient with the feature when you can't find a member.
[Code3:]
Of course, this feature can totally replace the currently For Else usage. See Code4:
[Code4:]
[Code5:]
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.
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)
}
[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)
}
[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!')
}
[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.')
}
}
Code: Select all
arr:=[]
for x in arr.__Enum(1){
OutputDebug(x)
}else if !A_Index{
MsgBox('The loop had zero iterations.')
}
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.