Bug in generic For-In-Next Loop?

Report any Hollywood bugs here
Post Reply
nexus
Posts: 150
Joined: Sun Mar 07, 2010 11:54 am

Bug in generic For-In-Next Loop?

Post 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
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

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

Post 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.
nexus
Posts: 150
Joined: Sun Mar 07, 2010 11:54 am

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

Post 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.
nexus
Posts: 150
Joined: Sun Mar 07, 2010 11:54 am

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

Post 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
Post Reply