Let's try to summarize:

1. The loop variable in a Vim9 script and a compiled function behave
   differently, we expect them to work the same way.  In a script the
   variable can't be found after the loop, it has been deleted.  In a
   compiled function it can be used by a closure executed later.
   -> In a script make the loop variable available also after the loop
      has finished.


2. Assuming the loop variable can be used, the value is what it was last
   set to, thus when using "for i in range(4)" it will be 3.  Other
   language do this too, thus it would be acceptable.  But it is not
   very useful (esp. since the variable is read-only, a closure cannot
   change it).  One exception: if the loop can be exited early, then the
   last value could be useful:
        for i in range(100)
           if i == 0
              timer_start(1000, (_) => {
                   echowin 'last count is ' i
                 })
           endif
           if i == 5
              break
           endif
        endfor
   -> Documenting this would be sufficient.


3. It is common to use the current value of the loop variable, not
   the final value.  There should be an easy way to do that.  That would
   be making a copy in a block-local variable (Python also mentions
   this).
   At the script level this works, but only by explicitly using a {}
   block, not by declaring the variable in the for-endfor block.
   In a compiled function this doesn't currently work.
   -> Make this work both in a script and compiled function:
        for i in range(4)
           var ii = i
           timer_start(1000, (_) => {
                echowin 'current count is ' ii
              })
        endfor
      This would show the values 0, 1, 2 and 3.

We could change the choice in the second one, but implementing that
efficiently actually isn't so easy.


-- 
The future isn't what it used to be.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20220911191417.630751C0CF3%40moolenaar.net.

Raspunde prin e-mail lui