@@ -125,6 +125,10 @@ that starts a comment: >
125125 var name = value # comment
126126 var name = value# error!
127127
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+
128132In legacy Vim script # is also used for the alternate file name. In Vim9
129133script you need to use %% instead. Instead of ## use %%% (stands for all
130134arguments).
@@ -164,6 +168,15 @@ list type, similar to TypeScript. For example, a list of numbers: >
164168 for item in itemlist
165169 ...
166170
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'
179+
167180
168181Functions and variables are script-local by default ~
169182 *vim9-scopes*
@@ -190,6 +203,12 @@ search for the function:
190203However, it is recommended to always use "g:" to refer to a global function
191204for clarity.
192205
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+
193212In all cases the function must be defined before used. That is when it is
194213called, when `:defcompile ` causes it to be compiled, or when code that calls
195214it is being compiled (to figure out the return type).
@@ -279,6 +298,9 @@ without any command. The same for global, window, tab, buffer and Vim
279298variables, because they are not really declared. They can also be deleted
280299with `:unlet ` .
281300
301+ `:lockvar ` does not work on local variables. Use `:const ` and `:final`
302+ instead.
303+
282304Variables, functions and function arguments cannot shadow previously defined
283305or imported variables and functions in the same script file.
284306Variables may shadow Ex commands, rename the variable if needed.
@@ -409,7 +431,18 @@ Additionally, a lambda can contain statements in {}: >
409431 g:was_called = 'yes'
410432 return expression
411433 }
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.
413446
414447 *vim9-curly*
415448To avoid the "{" of a dictionary literal to be recognized as a statement block
@@ -705,6 +738,7 @@ In legacy script this results in the character 0xc3 (an illegal byte), in Vim9
705738script this results in the string 'á'.
706739A negative index is counting from the end, "[-1]" is the last character.
707740To exclude the last character use | slice() | .
741+ To count composing characters separately use | strcharpart() | .
708742If the index is out of range then an empty string results.
709743
710744In legacy script "++var" and "--var" would be silently accepted and have no
@@ -972,6 +1006,8 @@ And classes and interfaces can be used as types: >
9721006 :var mine: MyInterface<string>
9731007 {not implemented yet}
9741008
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
9751011
9761012Variable types and type casting ~
9771013 *variable-types*
@@ -1044,6 +1080,27 @@ to a list of numbers.
10441080Same for | extend() | , use | extendnew() | instead, and for | flatten() | , use
10451081| flattennew() | instead.
10461082
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
1103+
10471104==============================================================================
10481105
104911065. Namespace, Import and Export
0 commit comments