Mercurial > vim
diff runtime/doc/vim9.txt @ 30202:fee9eccee266
Update runtime files
Commit: https://github.com/vim/vim/commit/71b6d3397649fed68ef587aa863fcbdf5fdb057a
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Sep 10 13:13:14 2022 +0100
Update runtime files
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 10 Sep 2022 14:15:06 +0200 |
parents | adb0de8be4ce |
children | 0827d3d6d8c0 |
line wrap: on
line diff
--- a/runtime/doc/vim9.txt +++ b/runtime/doc/vim9.txt @@ -1,4 +1,4 @@ -*vim9.txt* For Vim version 9.0. Last change: 2022 Jun 25 +*vim9.txt* For Vim version 9.0. Last change: 2022 Sep 10 VIM REFERENCE MANUAL by Bram Moolenaar @@ -1311,7 +1311,7 @@ Make sure to define the breakpoint befor The "inloop" variable will exist only once, all closures put in the list refer to the same instance, which in the end will have the value 4. This is efficient, also when looping many times. If you do want a separate context -for each closure call a function to define it: > +for each closure, call a function to define it: > def GetClosure(i: number): func var infunc = i return () => infunc @@ -1327,6 +1327,30 @@ for each closure call a function to defi In some situations, especially when calling a Vim9 closure from legacy context, the evaluation will fail. *E1248* +Note that at the script level the loop variable will be invalid after the +loop, also when used in a closure that is called later, e.g. with a timer. +This will generate error |E1302|: > + for n in range(4) + timer_start(500 * n, (_) => { + echowin n + }) + endfor + +You need to create a closure to store the current value of "n", so that it is +evaluated at the time the closure is created: > + def GetClosure(nr: number): func + return (_) => { + echowindow nr + } + enddef + + for n in range(4) + timer_start(500 * n, GetClosure(n)) + endfor + +Using `echowindow` is useful in a timer, the messages go into a popup and will +not interfere with what the user is doing when it triggers. + Converting a function from legacy to Vim9 ~ *convert_legacy_function_to_vim9* @@ -1618,7 +1642,7 @@ type, it can not be used in Vim9 script. *E1211* *E1217* *E1218* *E1219* *E1220* *E1221* *E1222* *E1223* *E1224* *E1225* *E1226* *E1227* *E1228* *E1238* *E1250* *E1251* *E1252* *E1253* - *E1256* *E1297* *E1298* + *E1256* *E1297* *E1298* *E1301* Types are checked for most builtin functions to make it easier to spot mistakes.