Page 1 of 1

Bug in generic For-In-Next Loop?

Posted: Sun May 19, 2024 11:05 pm
by nexus
From docu:
https://www.hollywood-mal.com/docs/html ... rgFor.html

Code: Select all

Function p_Test()
   Local months = {"January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"}
   revmonths = {}
   
   ;works not: 
   ;For Local i,v In IPairs(months)
   ;works:
   For i,v In IPairs(months)
     revmonths[v] = i + 1
   Next
 EndFunction
Bug:
1. The syntax highlighting in the vim plugin does not work for generic loops. The Author seems to have never used it. Shame on him! ;)
2. `Local` keyword cannot be used in `For-In-Next` loops
Too make the variables i,v local in the given example, they have to be declared before the loop


Cheers,
nexus

Re: Bug in generic For-In-Next Loop?

Posted: Mon May 20, 2024 6:11 pm
by airsoftsoftwair
Actually, I think there is a misunderstanding here. When using the generic for statement, the variables are always local. There is no way to use the generic for statement with non-local variables. That's why the "Local" keyword isn't supported for the generic for statement. Just check:

Code: Select all

Local months = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"}

For i,v In IPairs(months)
  DebugPrint(i,v)
Next

DebugPrint(i,v)
This will print:

Code: Select all

0 January
1 February
2 March
3 April
4 May
5 June
6 July
7 August
8 September
9 October
10 November
11 December
Nil Nil
As you can see, both "i" and "v" will be Nil as soon as the generic for loop has finished. They're only available within the loop.

Re: Bug in generic For-In-Next Loop?

Posted: Mon May 20, 2024 9:29 pm
by nexus
Thanks, Andreas.
That makes sense now.
Was not clear from the docu to me that the variables are always local. For the non-generic version, the behavior is different and that was the reason for believing that this is a bug.

Cheers,
Tom.

Re: Bug in generic For-In-Next Loop?

Posted: Thu Jun 20, 2024 3:37 pm
by nexus
Bug:
1. The syntax highlighting in the vim plugin does not work for generic loops. The Author seems to have never used it. Shame on him! ;)
That is going to be fixed soonish but I think, Ola Söder needs to accept the fix first. :)

https://github.com/vim/vim/pull/15059

:)

Cheers,
Tom