comparison runtime/doc/vim9.txt @ 24272:cabed216cc2f v8.2.2677

patch 8.2.2677: Vim9: cannot use only some of the default arguments Commit: https://github.com/vim/vim/commit/38a3bfa9a2931729a5e0c28dc087f745b68988ef Author: Bram Moolenaar <Bram@vim.org> Date: Mon Mar 29 22:14:55 2021 +0200 patch 8.2.2677: Vim9: cannot use only some of the default arguments Problem: Vim9: cannot use only some of the default arguments. Solution: Use v:none to use default argument value. Remove uf_def_arg_idx[], use JUMP_IF_ARG_SET. (closes #6504)
author Bram Moolenaar <Bram@vim.org>
date Mon, 29 Mar 2021 22:15:03 +0200
parents 4919f2d8d7fd
children 4ab4ef0c48b1
comparison
equal deleted inserted replaced
24271:b10dcae9e9a1 24272:cabed216cc2f
123 To improve readability there must be a space between a command and the # 123 To improve readability there must be a space between a command and the #
124 that starts a comment: > 124 that starts a comment: >
125 var name = value # comment 125 var name = value # comment
126 var name = value# error! 126 var name = value# error!
127 127
128 Do not start a comment with #{, it looks like the legacy dictionary literal
129 and produces an error where this might be confusing. #{{ or #{{{ are OK,
130 these can be used to start a fold.
131
128 In legacy Vim script # is also used for the alternate file name. In Vim9 132 In legacy Vim script # is also used for the alternate file name. In Vim9
129 script you need to use %% instead. Instead of ## use %%% (stands for all 133 script you need to use %% instead. Instead of ## use %%% (stands for all
130 arguments). 134 arguments).
131 135
132 136
161 Variable arguments are defined as the last argument, with a name and have a 165 Variable arguments are defined as the last argument, with a name and have a
162 list type, similar to TypeScript. For example, a list of numbers: > 166 list type, similar to TypeScript. For example, a list of numbers: >
163 def MyFunc(...itemlist: list<number>) 167 def MyFunc(...itemlist: list<number>)
164 for item in itemlist 168 for item in itemlist
165 ... 169 ...
170
171 When a function argument is optional (it has a default value) passing `v:none`
172 as the argument results in using the default value. This is useful when you
173 want to specify a value for an argument that comes after an argument that
174 should use its default value. Example: >
175 def MyFunc(one = 'one', last = 'last)
176 ...
177 enddef
178 MyFunc(v:none, 'LAST') # first argument uses default value 'one'
166 179
167 180
168 Functions and variables are script-local by default ~ 181 Functions and variables are script-local by default ~
169 *vim9-scopes* 182 *vim9-scopes*
170 When using `:function` or `:def` to specify a new function at the script level 183 When using `:function` or `:def` to specify a new function at the script level
188 - in the script scope, possibly imported 201 - in the script scope, possibly imported
189 - in the list of global functions 202 - in the list of global functions
190 However, it is recommended to always use "g:" to refer to a global function 203 However, it is recommended to always use "g:" to refer to a global function
191 for clarity. 204 for clarity.
192 205
206 Since a script-local function reference can be used without "s:" the name must
207 start with an upper case letter even when using the ":s" prefix. In legacy
208 script "s:funcref" could be used, because it could not be referred to with
209 "funcref". In Vim9 script it can, therefore "s:Funcref" must be used to avoid
210 that the name interferes with builtin functions.
211
193 In all cases the function must be defined before used. That is when it is 212 In all cases the function must be defined before used. That is when it is
194 called, when `:defcompile` causes it to be compiled, or when code that calls 213 called, when `:defcompile` causes it to be compiled, or when code that calls
195 it is being compiled (to figure out the return type). 214 it is being compiled (to figure out the return type).
196 215
197 The result is that functions and variables without a namespace can usually be 216 The result is that functions and variables without a namespace can usually be
276 295
277 In Vim9 script `:let` cannot be used. An existing variable is assigned to 296 In Vim9 script `:let` cannot be used. An existing variable is assigned to
278 without any command. The same for global, window, tab, buffer and Vim 297 without any command. The same for global, window, tab, buffer and Vim
279 variables, because they are not really declared. They can also be deleted 298 variables, because they are not really declared. They can also be deleted
280 with `:unlet`. 299 with `:unlet`.
300
301 `:lockvar` does not work on local variables. Use `:const` and `:final`
302 instead.
281 303
282 Variables, functions and function arguments cannot shadow previously defined 304 Variables, functions and function arguments cannot shadow previously defined
283 or imported variables and functions in the same script file. 305 or imported variables and functions in the same script file.
284 Variables may shadow Ex commands, rename the variable if needed. 306 Variables may shadow Ex commands, rename the variable if needed.
285 307
407 Additionally, a lambda can contain statements in {}: > 429 Additionally, a lambda can contain statements in {}: >
408 var Lambda = (arg) => { 430 var Lambda = (arg) => {
409 g:was_called = 'yes' 431 g:was_called = 'yes'
410 return expression 432 return expression
411 } 433 }
412 NOT IMPLEMENTED YET 434
435 The ending "}" must be at the start of a line. It can be followed by other
436 characters, e.g.: >
437 var d = mapnew(dict, (k, v): string => {
438 return 'value'
439 })
440 No command can follow the "{", only a comment can be used there.
441
442 Rationale: The "}" cannot be after a command because it would require parsing
443 the commands to find it. For consistency with that no command can follow the
444 "{". Unfortunately this means using "() => { command }" does not work, line
445 breaks are always required.
413 446
414 *vim9-curly* 447 *vim9-curly*
415 To avoid the "{" of a dictionary literal to be recognized as a statement block 448 To avoid the "{" of a dictionary literal to be recognized as a statement block
416 wrap it in parentheses: > 449 wrap it in parentheses: >
417 var Lambda = (arg) => ({key: 42}) 450 var Lambda = (arg) => ({key: 42})
703 echo 'bár'[1] 736 echo 'bár'[1]
704 In legacy script this results in the character 0xc3 (an illegal byte), in Vim9 737 In legacy script this results in the character 0xc3 (an illegal byte), in Vim9
705 script this results in the string 'á'. 738 script this results in the string 'á'.
706 A negative index is counting from the end, "[-1]" is the last character. 739 A negative index is counting from the end, "[-1]" is the last character.
707 To exclude the last character use |slice()|. 740 To exclude the last character use |slice()|.
741 To count composing characters separately use |strcharpart()|.
708 If the index is out of range then an empty string results. 742 If the index is out of range then an empty string results.
709 743
710 In legacy script "++var" and "--var" would be silently accepted and have no 744 In legacy script "++var" and "--var" would be silently accepted and have no
711 effect. This is an error in Vim9 script. 745 effect. This is an error in Vim9 script.
712 746
970 :class MyInterface<Targ> 1004 :class MyInterface<Targ>
971 :var mine: MyInterface<number> 1005 :var mine: MyInterface<number>
972 :var mine: MyInterface<string> 1006 :var mine: MyInterface<string>
973 {not implemented yet} 1007 {not implemented yet}
974 1008
1009 You may also find this wiki useful. It was written by an early adoptor of
1010 Vim9 script: https://github.com/lacygoill/wiki/blob/master/vim/vim9.md
975 1011
976 Variable types and type casting ~ 1012 Variable types and type casting ~
977 *variable-types* 1013 *variable-types*
978 Variables declared in Vim9 script or in a `:def` function have a type, either 1014 Variables declared in Vim9 script or in a `:def` function have a type, either
979 specified explicitly or inferred from the initialization. 1015 specified explicitly or inferred from the initialization.
1041 Instead use |mapnew()|. If the item type was determined to be "any" it can 1077 Instead use |mapnew()|. If the item type was determined to be "any" it can
1042 change to a more specific type. E.g. when a list of mixed types gets changed 1078 change to a more specific type. E.g. when a list of mixed types gets changed
1043 to a list of numbers. 1079 to a list of numbers.
1044 Same for |extend()|, use |extendnew()| instead, and for |flatten()|, use 1080 Same for |extend()|, use |extendnew()| instead, and for |flatten()|, use
1045 |flattennew()| instead. 1081 |flattennew()| instead.
1082
1083 Closures defined in a loop will share the same context. For example: >
1084 var flist: list<func>
1085 for i in range(10)
1086 var inloop = i
1087 flist[i] = () => inloop
1088 endfor
1089
1090 The "inloop" variable will exist only once, all closures put in the list refer
1091 to the same instance, which in the end will have the value 9. This is
1092 efficient. If you do want a separate context for each closure call a function
1093 to define it: >
1094 def GetFunc(i: number): func
1095 var inloop = i
1096 return () => inloop
1097 enddef
1098
1099 var flist: list<func>
1100 for i in range(10)
1101 flist[i] = GetFunc(i)
1102 endfor
1046 1103
1047 ============================================================================== 1104 ==============================================================================
1048 1105
1049 5. Namespace, Import and Export 1106 5. Namespace, Import and Export
1050 *vim9script* *vim9-export* *vim9-import* 1107 *vim9script* *vim9-export* *vim9-import*