Mercurial > vim
comparison runtime/doc/vim9.txt @ 27623:179c118424a6
Update runtime files
Commit: https://github.com/vim/vim/commit/9da17d7c57071c306565da6a35c3704db1916b78
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Feb 9 21:50:44 2022 +0000
Update runtime files
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 09 Feb 2022 23:00:06 +0100 |
parents | 063952f68595 |
children | 9fe2fed9bb4b |
comparison
equal
deleted
inserted
replaced
27622:e33b6abfe384 | 27623:179c118424a6 |
---|---|
1 *vim9.txt* For Vim version 8.2. Last change: 2022 Feb 04 | 1 *vim9.txt* For Vim version 8.2. Last change: 2022 Feb 09 |
2 | 2 |
3 | 3 |
4 VIM REFERENCE MANUAL by Bram Moolenaar | 4 VIM REFERENCE MANUAL by Bram Moolenaar |
5 | 5 |
6 | 6 |
219 Functions and variables are script-local by default ~ | 219 Functions and variables are script-local by default ~ |
220 *vim9-scopes* | 220 *vim9-scopes* |
221 When using `:function` or `:def` to specify a new function at the script level | 221 When using `:function` or `:def` to specify a new function at the script level |
222 in a Vim9 script, the function is local to the script, as if "s:" was | 222 in a Vim9 script, the function is local to the script, as if "s:" was |
223 prefixed. Using the "s:" prefix is optional. To define a global function or | 223 prefixed. Using the "s:" prefix is optional. To define a global function or |
224 variable the "g:" prefix must be used. For functions in an autoload script | 224 variable the "g:" prefix must be used. For functions in a script that is to |
225 the "name#" prefix is sufficient. > | 225 be imported and in an autoload script "export" needs to be used. > |
226 def ThisFunction() # script-local | 226 def ThisFunction() # script-local |
227 def s:ThisFunction() # script-local | 227 def s:ThisFunction() # script-local |
228 def g:ThatFunction() # global | 228 def g:ThatFunction() # global |
229 def scriptname#function() # autoload | 229 export def Function() # for import and import autoload |
230 < *E1058* *E1075* | 230 < *E1058* *E1075* |
231 When using `:function` or `:def` to specify a nested function inside a `:def` | 231 When using `:function` or `:def` to specify a nested function inside a `:def` |
232 function and no namespace was given, this nested function is local to the code | 232 function and no namespace was given, this nested function is local to the code |
233 block it is defined in. In a `:def` function it is not possible to define a | 233 block it is defined in. In a `:def` function it is not possible to define a |
234 script-local function. It is possible to define a global function by using | 234 script-local function. It is possible to define a global function by using |
278 | 278 |
279 You want to use this in scripts that use a `finish` command to bail out at | 279 You want to use this in scripts that use a `finish` command to bail out at |
280 some point when loaded again. E.g. when a buffer local option is set: > | 280 some point when loaded again. E.g. when a buffer local option is set: > |
281 vim9script noclear | 281 vim9script noclear |
282 setlocal completefunc=SomeFunc | 282 setlocal completefunc=SomeFunc |
283 if exists('*g:SomeFunc') | finish | endif | 283 if exists('*g:SomeFunc') |
284 finish | |
285 endif | |
284 def g:SomeFunc() | 286 def g:SomeFunc() |
285 .... | 287 .... |
286 | 288 |
287 | 289 |
288 Variable declarations with :var, :final and :const ~ | 290 Variable declarations with :var, :final and :const ~ |
1396 list<func(...)> | 1398 list<func(...)> |
1397 | 1399 |
1398 For script-local variables in Vim9 script the type is checked, also when the | 1400 For script-local variables in Vim9 script the type is checked, also when the |
1399 variable was declared in a legacy function. | 1401 variable was declared in a legacy function. |
1400 | 1402 |
1401 When a type has been declared this is attached to a list or string. When | 1403 When a type has been declared this is attached to a List or Dictionary. When |
1402 later some expression attempts to change the type an error will be given: > | 1404 later some expression attempts to change the type an error will be given: > |
1403 var ll: list<number> = [1, 2, 3] | 1405 var ll: list<number> = [1, 2, 3] |
1404 ll->extend(['x']) # Error, 'x' is not a number | 1406 ll->extend(['x']) # Error, 'x' is not a number |
1405 | 1407 |
1406 If the type is inferred then the type is allowed to change: > | 1408 If the type is not declared then it is allowed to change: > |
1407 [1, 2, 3]->extend(['x']) # result: [1, 2, 3, 'x'] | 1409 [1, 2, 3]->extend(['x']) # result: [1, 2, 3, 'x'] |
1410 | |
1411 For a variable declaration an inferred type matters: > | |
1412 var ll = [1, 2, 3] | |
1413 ll->extend(['x']) # Error, 'x' is not a number | |
1414 That is because the declaration looks like a list of numbers, thus is | |
1415 equivalent to: > | |
1416 var ll: list<number> = [1, 2, 3] | |
1417 If you do want a more permissive list you need to declare the type: > | |
1418 var ll: list<any = [1, 2, 3] | |
1419 ll->extend(['x']) # OK | |
1408 | 1420 |
1409 | 1421 |
1410 Stricter type checking ~ | 1422 Stricter type checking ~ |
1411 *type-checking* | 1423 *type-checking* |
1412 In legacy Vim script, where a number was expected, a string would be | 1424 In legacy Vim script, where a number was expected, a string would be |