blob: 704e801c26a6e98b2728d16085d7b3f264d90536 [file] [log] [blame]
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +02001*vim9.txt* For Vim version 8.2. Last change: 2020 Apr 19
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
8
9Vim9 script commands and expressions.
10
11Most expression help is in |eval.txt|. This file is about the new syntax and
12features in Vim9 script.
13
14THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
15
16
171 What is Vim9 script? |vim9-script|
182. Differences |vim9-differences|
193. New style functions |fast-functions|
204. Types |vim9-types|
215. Namespace, Import and Export |vim9script|
22
239. Rationale |vim9-rationale|
24
25==============================================================================
26
271. What is Vim9 script? *vim9-script*
28
29THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
30
31Vim script has been growing over time, while keeping backwards compatibility.
32That means bad choices from the past often can't be changed. Execution is
33quite slow, every line is parsed every time it is executed.
34
35The main goal of Vim9 script is to drastically improve performance. An
36increase in execution speed of 10 to 100 times can be expected. A secondary
37goal is to avoid Vim-specific constructs and get closer to commonly used
38programming languages, such as JavaScript, TypeScript and Java.
39
40The performance improvements can only be achieved by not being 100% backwards
41compatible. For example, in a function the arguments are not available in the
42"a:" dictionary, as creating that dictionary adds quite a lot of overhead.
43Other differences are more subtle, such as how errors are handled.
44
45The Vim9 script syntax and semantics are used in:
46- a function defined with the `:def` command
47- a script file where the first command is `vim9script`
48
49When using `:function` in a Vim9 script file the legacy syntax is used.
50However, this is discouraged.
51
52Vim9 script and legacy Vim script can be mixed. There is no need to rewrite
53old scripts, they keep working as before.
54
55==============================================================================
56
572. Differences from legacy Vim script *vim9-differences*
58
59THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
60
Bram Moolenaar2c330432020-04-13 14:41:35 +020061Comments starting with # ~
62
63In Vim script comments normally start with double quote. That can also be the
64start of a string, thus in many places it cannot be used. In Vim9 script a
65comment can also start with #. Normally this is a command to list text with
66numbers, but you can also use `:number` for that. >
67 let count = 0 # number of occurences of Ni!
68
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020069To improve readability there must be a space between the command and the #
70that starts a comment. Note that #{ is the start of a dictionary, therefore
71it cannot start a comment.
72
Bram Moolenaar2c330432020-04-13 14:41:35 +020073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074Vim9 functions ~
75
76`:def` has no extra arguments like `:function` does: "range", "abort", "dict"
77or "closure". A `:def` function always aborts on an error, does not get a
78range passed and cannot be a "dict" function.
79
80In the function body:
81- Arguments are accessed by name, without "a:".
82- There is no "a:" dictionary or "a:000" list. Variable arguments are defined
83 with a name and have a list type: >
84 def MyFunc(...itemlist: list<type>)
85 for item in itemlist
86 ...
87
88
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020089Functions are script-local by default ~
90
91When using `:function` or `:def` to specify a new function at the script level
92in a Vim9 script, the function is local to the script, as if "s:" was
93prefixed. To define a global function the "g:" prefix must be used.
94
95When using `:function` or `:def` to specify a new function inside a function,
96the function is local to the function. It is not possible to define a
97script-local function inside a function. To define a global function the "g:"
98prefix must be used.
99
100When referring to a function and no "s:" or "g:" prefix is used, Vim will
101search for the function in this order:
102- Local to the current function scope.
103- Local to the current script file.
104- Imported functions, see `:import`.
105- Global.
106
107Global functions can be defined and deleted at nearly any time. In Vim9
108script script-local functions are defined once when the script is sourced and
109cannot be deleted.
110
111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112Variable declarations with :let and :const ~
113
114Local variables need to be declared with `:let`. Local constants need to be
115declared with `:const`. We refer to both as "variables".
116
117Variables can be local to a script, function or code block: >
118 vim9script
119 let script_var = 123
120 def SomeFunc()
121 let func_var = script_var
122 if cond
123 let block_var = func_var
124 ...
125
126The variables are only visible in the block where they are defined and nested
127blocks. Once the block ends the variable is no longer accessible: >
128 if cond
129 let inner = 5
130 else
131 let inner = 0
132 endif
133 echo inner " Error!
134
135The declaration must be done earlier: >
136 let inner: number
137 if cond
138 inner = 5
139 else
140 inner = 0
141 endif
142 echo inner
143
144To intentionally use a variable that won't be available later, a block can be
145used: >
146 {
147 let temp = 'temp'
148 ...
149 }
150 echo temp " Error!
151
Bram Moolenaar560979e2020-02-04 22:53:05 +0100152An existing variable cannot be assigned to with `:let`, since that implies a
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153declaration. An exception is global variables: these can be both used with
154and without `:let`, because there is no rule about where they are declared.
155
156Variables cannot shadow previously defined variables.
157Variables may shadow Ex commands, rename the variable if needed.
158
Bram Moolenaard1caa942020-04-10 22:10:56 +0200159Global variables must be prefixed with "g:", also at the script level.
160However, global user defined functions are used without "g:". >
161 vim9script
162 let script_local = 'text'
163 let g:global = 'value'
164 let Funcref = ThatFunction
165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be
167used to repeat a `:substitute` command.
168
169
170Omitting :call and :eval ~
171
172Functions can be called without `:call`: >
173 writefile(lines, 'file')
Bram Moolenaar560979e2020-02-04 22:53:05 +0100174Using `:call` is still possible, but this is discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175
176A method call without `eval` is possible, so long as the start is an
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100177identifier or can't be an Ex command. It does NOT work for string constants: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 myList->add(123) " works
179 g:myList->add(123) " works
180 [1, 2, 3]->Process() " works
181 #{a: 1, b: 2}->Process() " works
182 {'a': 1, 'b': 2}->Process() " works
183 "foobar"->Process() " does NOT work
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100184 ("foobar")->Process() " works
185 'foobar'->Process() " does NOT work
186 ('foobar')->Process() " works
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100187
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100188In case there is ambiguity between a function name and an Ex command, use ":"
189to make clear you want to use the Ex command. For example, there is both the
190`:substitute` command and the `substitute()` function. When the line starts
191with `substitute(` this will use the function, prepend a colon to use the
192command instead: >
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100193 :substitute(pattern (replacement (
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100194
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100195Note that while variables need to be defined before they can be used,
196functions can be called before being defined. This is required to be able
197have cyclic dependencies between functions. It is slightly less efficient,
198since the function has to be looked up by name. And a typo in the function
199name will only be found when the call is executed.
200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100201
Bram Moolenaard1caa942020-04-10 22:10:56 +0200202Omitting function() ~
203
204A user defined function can be used as a function reference in an expression
205without `function()`. The argument types and return type will then be checked.
206The function must already have been defined. >
207
208 let Funcref = MyFunction
209
210When using `function()` the resulting type is "func", a function with any
211number of arguments and any return type. The function can be defined later.
212
213
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200214Automatic line continuation ~
215
216In many cases it is obvious that an expression continues on the next line. In
217those cases there is no need to prefix the line with a backslash. For
218example, when a list spans multiple lines: >
219 let mylist = [
220 'one',
221 'two',
222 ]
Bram Moolenaare6085c52020-04-12 20:19:16 +0200223And when a dict spans multiple lines: >
224 let mydict = #{
225 one: 1,
226 two: 2,
227 }
228Function call: >
229 let result = Func(
230 arg1,
231 arg2
232 )
233
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200234For binary operators iin expressions not in [], {} or () a line break is
235possible AFTER the operators. For example: >
236 let text = lead ..
237 middle ..
238 end
239 let total = start +
240 end -
241 correction
242 let result = positive ?
243 PosFunc(arg) :
244 NegFunc(arg)
245
Bram Moolenaare6085c52020-04-12 20:19:16 +0200246Note that "enddef" cannot be used at the start of a continuation line, it ends
247the current function.
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200248
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200249It is also possible to split a function header over multiple lines, in between
250arguments: >
251 def MyFunc(
252 text: string,
253 separator = '-'
254 ): string
255
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257No curly braces expansion ~
258
259|curly-braces-names| cannot be used.
260
261
Bram Moolenaar560979e2020-02-04 22:53:05 +0100262No :append, :change or :insert ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263
Bram Moolenaar560979e2020-02-04 22:53:05 +0100264These commands are too quickly confused with local variable names.
265
266
267Comparators ~
268
269The 'ignorecase' option is not used for comparators that use strings.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270
271
272White space ~
273
274Vim9 script enforces proper use of white space. This is no longer allowed: >
275 let var=234 " Error!
276 let var= 234 " Error!
277 let var =234 " Error!
278There must be white space before and after the "=": >
279 let var = 234 " OK
Bram Moolenaar2c330432020-04-13 14:41:35 +0200280White space must also be put before the # that starts a comment: >
281 let var = 234# Error!
282 let var = 234 # OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283
284White space is required around most operators.
285
286White space is not allowed:
287- Between a function name and the "(": >
288 call Func (arg) " Error!
289 call Func
290 \ (arg) " Error!
291 call Func(arg) " OK
292 call Func(
293 \ arg) " OK
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100294 call Func(
295 \ arg " OK
296 \ )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
298
299Conditions and expressions ~
300
301Conditions and expression are mostly working like they do in JavaScript. A
302difference is made where JavaScript does not work like most people expect.
303Specifically, an empty list is falsey.
304
305Any type of variable can be used as a condition, there is no error, not even
306for using a list or job. This is very much like JavaScript, but there are a
307few exceptions.
308
309 type TRUE when ~
310 bool v:true
311 number non-zero
312 float non-zero
313 string non-empty
314 blob non-empty
315 list non-empty (different from JavaScript)
316 dictionary non-empty (different from JavaScript)
Bram Moolenaard1caa942020-04-10 22:10:56 +0200317 func when there is a function name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 special v:true
319 job when not NULL
320 channel when not NULL
321 class when not NULL
322 object when not NULL (TODO: when isTrue() returns v:true)
323
324The boolean operators "||" and "&&" do not change the value: >
325 8 || 2 == 8
326 0 || 2 == 2
327 0 || '' == ''
328 8 && 2 == 2
329 0 && 2 == 0
330 [] && 2 == []
331
332When using `..` for string concatenation the arguments are always converted to
333string. >
334 'hello ' .. 123 == 'hello 123'
335 'hello ' .. v:true == 'hello true'
336
337In Vim9 script one can use "true" for v:true and "false" for v:false.
338
339
340==============================================================================
341
3423. New style functions *fast-functions*
343
344THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
345
346 *:def*
347:def[!] {name}([arguments])[: {return-type}
348 Define a new function by the name {name}. The body of
349 the function follows in the next lines, until the
350 matching `:enddef`.
351
Bram Moolenaard77a8522020-04-03 21:59:57 +0200352 When {return-type} is omitted or is "void" the
353 function is not expected to return anything.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100354
355 {arguments} is a sequence of zero or more argument
356 declarations. There are three forms:
357 {name}: {type}
358 {name} = {value}
359 {name}: {type} = {value}
360 The first form is a mandatory argument, the caller
361 must always provide them.
362 The second and third form are optional arguments.
363 When the caller omits an argument the {value} is used.
364
Bram Moolenaar560979e2020-02-04 22:53:05 +0100365 NOTE: It is possible to nest `:def` inside another
366 `:def`, but it is not possible to nest `:def` inside
367 `:function`, for backwards compatibility.
368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 [!] is used as with `:function`.
370
371 *:enddef*
372:enddef End of a function defined with `:def`.
373
374
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100375If the script the function is defined in is Vim9 script, then script-local
376variables can be accessed without the "s:" prefix. They must be defined
377before the function. If the script the function is defined in is legacy
378script, then script-local variables must be accessed with the "s:" prefix.
379
380
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100381 *:disa* *:disassemble*
382:disa[ssemble] {func} Show the instructions generated for {func}.
383 This is for debugging and testing.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100384 Note that for command line completion of {func} you
385 can prepend "s:" to find script-local functions.
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387==============================================================================
388
3894. Types *vim9-types*
390
391THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
392
393The following builtin types are supported:
394 bool
395 number
396 float
397 string
398 blob
Bram Moolenaard77a8522020-04-03 21:59:57 +0200399 list<{type}>
400 dict<{type}>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 job
402 channel
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100403 func
Bram Moolenaard1caa942020-04-10 22:10:56 +0200404 func: {type}
Bram Moolenaard77a8522020-04-03 21:59:57 +0200405 func({type}, ...)
406 func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407
408Not supported yet:
Bram Moolenaard77a8522020-04-03 21:59:57 +0200409 tuple<a: {type}, b: {type}, ...>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100410
Bram Moolenaard77a8522020-04-03 21:59:57 +0200411These types can be used in declarations, but no value will have this type:
412 {type}|{type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 void
414 any
415
Bram Moolenaard77a8522020-04-03 21:59:57 +0200416There is no array type, use list<{type}> instead. For a list constant an
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100417efficient implementation is used that avoids allocating lot of small pieces of
418memory.
419
Bram Moolenaard77a8522020-04-03 21:59:57 +0200420A partial and function can be declared in more or less specific ways:
421func any kind of function reference, no type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200422 checking for arguments or return value
Bram Moolenaard77a8522020-04-03 21:59:57 +0200423func: {type} any number and type of arguments with specific
424 return type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200425func({type}) function with argument type, does not return
Bram Moolenaard77a8522020-04-03 21:59:57 +0200426 a value
Bram Moolenaard1caa942020-04-10 22:10:56 +0200427func({type}): {type} function with argument type and return type
428func(?{type}) function with type of optional argument, does
429 not return a value
430func(...{type}) function with type of variable number of
431 arguments, does not return a value
432func({type}, ?{type}, ...{type}): {type}
433 function with:
434 - type of mandatory argument
435 - type of optional argument
436 - type of variable number of arguments
437 - return type
Bram Moolenaard77a8522020-04-03 21:59:57 +0200438
439If the return type is "void" the function does not return a value.
440
441The reference can also be a |Partial|, in which case it stores extra arguments
442and/or a dictionary, which are not visible to the caller. Since they are
443called in the same way the declaration is the same.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100444
445Custom types can be defined with `:type`: >
446 :type MyList list<string>
447{not implemented yet}
448
449And classes and interfaces can be used as types: >
450 :class MyClass
451 :let mine: MyClass
452
453 :interface MyInterface
454 :let mine: MyInterface
455
456 :class MyTemplate<Targ>
457 :let mine: MyTemplate<number>
458 :let mine: MyTemplate<string>
459
460 :class MyInterface<Targ>
461 :let mine: MyInterface<number>
462 :let mine: MyInterface<string>
463{not implemented yet}
464
465
466Type inference *type-inference*
467
468In general: Whenever the type is clear it can be omitted. For example, when
469declaring a variable and giving it a value: >
470 let var = 0 " infers number type
471 let var = 'hello' " infers string type
472
473
474==============================================================================
475
4765. Namespace, Import and Export
477 *vim9script* *vim9-export* *vim9-import*
478
479THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
480
481A Vim9 script can be written to be imported. This means that everything in
482the script is local, unless exported. Those exported items, and only those
483items, can then be imported in another script.
484
485
486Namespace ~
487 *:vim9script* *:vim9*
Bram Moolenaar560979e2020-02-04 22:53:05 +0100488To recognize a file that can be imported the `vim9script` statement must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489appear as the first statement in the file. It tells Vim to interpret the
490script in its own namespace, instead of the global namespace. If a file
491starts with: >
492 vim9script
493 let myvar = 'yes'
494Then "myvar" will only exist in this file. While without `vim9script` it would
495be available as `g:myvar` from any other script and function.
496
497The variables at the file level are very much like the script-local "s:"
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200498variables in legacy Vim script, but the "s:" is omitted. And they cannot be
499deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200501In Vim9 script the global "g:" namespace can still be used as before. And the
502"w:", "b:" and "t:" namespaces. These have in common that variables are not
503declared and they can be deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504
505A side effect of `:vim9script` is that the 'cpoptions' option is set to the
506Vim default value, like with: >
507 :set cpo&vim
508One of the effects is that |line-continuation| is always enabled.
509The original value of 'cpoptions' is restored at the end of the script.
510
511
512Export ~
513 *:export* *:exp*
514Exporting one item can be written as: >
515 export const EXPORTED_CONST = 1234
516 export let someValue = ...
517 export def MyFunc() ...
518 export class MyClass ...
519
520As this suggests, only constants, variables, `:def` functions and classes can
521be exported.
522
523Alternatively, an export statement can be used to export several already
524defined (otherwise script-local) items: >
525 export {EXPORTED_CONST, someValue, MyFunc, MyClass}
526
527
528Import ~
529 *:import* *:imp*
530The exported items can be imported individually in another Vim9 script: >
531 import EXPORTED_CONST from "thatscript.vim"
532 import MyClass from "myclass.vim"
533
534To import multiple items at the same time: >
535 import {someValue, MyClass} from "thatscript.vim"
536
Bram Moolenaar560979e2020-02-04 22:53:05 +0100537In case the name is ambiguous, another name can be specified: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538 import MyClass as ThatClass from "myclass.vim"
539 import {someValue, MyClass as ThatClass} from "myclass.vim"
540
541To import all exported items under a specific identifier: >
542 import * as That from 'thatscript.vim'
543
544Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free
545to choose the name "That", but it is highly recommended to use the name of the
546script file to avoid confusion.
547
548The script name after `import` can be:
549- A relative path, starting "." or "..". This finds a file relative to the
550 location of the script file itself. This is useful to split up a large
551 plugin into several files.
552- An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This
553 will be rarely used.
554- A path not being relative or absolute. This will be found in the
555 "import" subdirectories of 'runtimepath' entries. The name will usually be
556 longer and unique, to avoid loading the wrong file.
557
558Once a vim9 script file has been imported, the result is cached and used the
559next time the same script is imported. It will not be read again.
560 *:import-cycle*
561The `import` commands are executed when encountered. If that script (directly
562or indirectly) imports the current script, then items defined after the
563`import` won't be processed yet. Therefore cyclic imports can exist, but may
564result in undefined items.
565
566
567Import in an autoload script ~
568
569For optimal startup speed, loading scripts should be postponed until they are
Bram Moolenaar560979e2020-02-04 22:53:05 +0100570actually needed. A recommended mechanism:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571
5721. In the plugin define user commands, functions and/or mappings that refer to
573 an autoload script. >
574 command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>)
575
576< This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen.
577
5782. In the autocommand script do the actual work. You can import items from
579 other files to split up functionality in appropriate pieces. >
580 vim9script
581 import FilterFunc from "../import/someother.vim"
582 def searchfor#Stuff(arg: string)
583 let filtered = FilterFunc(arg)
584 ...
585< This goes in .../autoload/searchfor.vim. "searchfor" in the file name
586 must be exactly the same as the prefix for the function name, that is how
587 Vim finds the file.
588
5893. Other functionality, possibly shared between plugins, contains the exported
590 items and any private items. >
591 vim9script
592 let localVar = 'local'
593 export def FilterFunc(arg: string): string
594 ...
595< This goes in .../import/someother.vim.
596
597
598Import in legacy Vim script ~
599
600If an `import` statement is used in legacy Vim script, for identifier the
601script-local "s:" namespace will be used, even when "s:" is not specified.
602
603
604==============================================================================
605
6069. Rationale *vim9-rationale*
607
608The :def command ~
609
610Plugin writers have asked for a much faster Vim script. Investigation have
Bram Moolenaar560979e2020-02-04 22:53:05 +0100611shown that keeping the existing semantics of function calls make this close to
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612impossible, because of the overhead involved with calling a function, setting
613up the local function scope and executing lines. There are many details that
614need to be handled, such as error messages and exceptions. The need to create
615a dictionary for a: and l: scopes, the a:000 list and several others add too
616much overhead that cannot be avoided.
617
618Therefore the `:def` method to define a new-style function had to be added,
619which allows for a function with different semantics. Most things still work
620as before, but some parts do not. A new way to define a function was
621considered the best way to separate the old-style code from Vim9 script code.
622
623Using "def" to define a function comes from Python. Other languages use
624"function" which clashes with legacy Vim script.
625
626
627Type checking ~
628
629When compiling lines of Vim commands into instructions as much as possible
630should be done at compile time. Postponing it to runtime makes the execution
631slower and means mistakes are found only later. For example, when
632encountering the "+" character and compiling this into a generic add
633instruction, at execution time the instruction would have to inspect the type
634of the arguments and decide what kind of addition to do. And when the
635type is dictionary throw an error. If the types are known to be numbers then
636an "add number" instruction can be used, which is faster. The error can be
637given at compile time, no error handling is needed at runtime.
638
639The syntax for types is similar to Java, since it is easy to understand and
640widely used. The type names are what was used in Vim before, with some
641additions such as "void" and "bool".
642
643
644JavaScript/TypeScript syntax and semantics ~
645
646Script writers have complained that the Vim script syntax is unexpectedly
647different from what they are used to. To reduce this complaint popular
648languages will be used as an example. At the same time, we do not want to
Bram Moolenaar560979e2020-02-04 22:53:05 +0100649abandon the well-known parts of legacy Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650
651Since Vim already uses `:let` and `:const` and optional type checking is
652desirable, the JavaScript/TypeScript syntax fits best for variable
653declarations. >
654 const greeting = 'hello' " string type is inferred
655 let name: string
656 ...
657 name = 'John'
658
659Expression evaluation was already close to what JavaScript and other languages
660are doing. Some details are unexpected and can be fixed. For example how the
661|| and && operators work. Legacy Vim script: >
662 let result = 44
663 ...
664 return result || 0 " returns 1
665
666Vim9 script works like JavaScript, keep the value: >
667 let result = 44
668 ...
669 return result || 0 " returns 44
670
671On the other hand, overloading "+" to use both for addition and string
672concatenation goes against legacy Vim script and often leads to mistakes.
673For that reason we will keep using ".." for string concatenation. Lua also
674uses ".." this way.
675
676
677Import and Export ~
678
679A problem of legacy Vim script is that by default all functions and variables
680are global. It is possible to make them script-local, but then they are not
681available in other scripts.
682
683In Vim9 script a mechanism very similar to the Javascript import and export
684mechanism is supported. It is a variant to the existing `:source` command
685that works like one would expect:
686- Instead of making everything global by default, everything is script-local,
687 unless exported.
688- When importing a script the symbols that are imported are listed, avoiding
689 name conflicts and failures if later functionality is added.
690- The mechanism allows for writing a big, long script with a very clear API:
691 the exported function(s) and class(es).
692- By using relative paths loading can be much faster for an import inside of a
693 package, no need to search many directories.
694- Once an import has been used, it can be cached and loading it again can be
695 avoided.
696- The Vim-specific use of "s:" to make things script-local can be dropped.
697
698
699Classes ~
700
701Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But
702these have never become widespread. When Vim 9 was designed a decision was
703made to phase out these interfaces and concentrate on Vim script, while
704encouraging plugin authors to write code in any language and run it as an
705external tool, using jobs and channels.
706
707Still, using an external tool has disadvantages. An alternative is to convert
708the tool into Vim script. For that to be possible without too much
709translation, and keeping the code fast at the same time, the constructs of the
710tool need to be supported. Since most languages support classes the lack of
711class support in Vim is then a problem.
712
713Previously Vim supported a kind-of object oriented programming by adding
714methods to a dictionary. With some care this could be made to work, but it
715does not look like real classes. On top of that, it's very slow, because of
716the use of dictionaries.
717
718The support of classes in Vim9 script is a "minimal common functionality" of
719class support in most languages. It works mostly like Java, which is the most
720popular programming language.
721
722
723
724 vim:tw=78:ts=8:noet:ft=help:norl: