blob: dd2d5105656aa9a5087165a0eda6a62a2586cf76 [file] [log] [blame]
Bram Moolenaard1caa942020-04-10 22:10:56 +02001*vim9.txt* For Vim version 8.2. Last change: 2020 Apr 09
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
69
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010070Vim9 functions ~
71
72`:def` has no extra arguments like `:function` does: "range", "abort", "dict"
73or "closure". A `:def` function always aborts on an error, does not get a
74range passed and cannot be a "dict" function.
75
76In the function body:
77- Arguments are accessed by name, without "a:".
78- There is no "a:" dictionary or "a:000" list. Variable arguments are defined
79 with a name and have a list type: >
80 def MyFunc(...itemlist: list<type>)
81 for item in itemlist
82 ...
83
84
85Variable declarations with :let and :const ~
86
87Local variables need to be declared with `:let`. Local constants need to be
88declared with `:const`. We refer to both as "variables".
89
90Variables can be local to a script, function or code block: >
91 vim9script
92 let script_var = 123
93 def SomeFunc()
94 let func_var = script_var
95 if cond
96 let block_var = func_var
97 ...
98
99The variables are only visible in the block where they are defined and nested
100blocks. Once the block ends the variable is no longer accessible: >
101 if cond
102 let inner = 5
103 else
104 let inner = 0
105 endif
106 echo inner " Error!
107
108The declaration must be done earlier: >
109 let inner: number
110 if cond
111 inner = 5
112 else
113 inner = 0
114 endif
115 echo inner
116
117To intentionally use a variable that won't be available later, a block can be
118used: >
119 {
120 let temp = 'temp'
121 ...
122 }
123 echo temp " Error!
124
Bram Moolenaar560979e2020-02-04 22:53:05 +0100125An existing variable cannot be assigned to with `:let`, since that implies a
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126declaration. An exception is global variables: these can be both used with
127and without `:let`, because there is no rule about where they are declared.
128
129Variables cannot shadow previously defined variables.
130Variables may shadow Ex commands, rename the variable if needed.
131
Bram Moolenaard1caa942020-04-10 22:10:56 +0200132Global variables must be prefixed with "g:", also at the script level.
133However, global user defined functions are used without "g:". >
134 vim9script
135 let script_local = 'text'
136 let g:global = 'value'
137 let Funcref = ThatFunction
138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be
140used to repeat a `:substitute` command.
141
142
143Omitting :call and :eval ~
144
145Functions can be called without `:call`: >
146 writefile(lines, 'file')
Bram Moolenaar560979e2020-02-04 22:53:05 +0100147Using `:call` is still possible, but this is discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148
149A method call without `eval` is possible, so long as the start is an
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100150identifier or can't be an Ex command. It does NOT work for string constants: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 myList->add(123) " works
152 g:myList->add(123) " works
153 [1, 2, 3]->Process() " works
154 #{a: 1, b: 2}->Process() " works
155 {'a': 1, 'b': 2}->Process() " works
156 "foobar"->Process() " does NOT work
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100157 ("foobar")->Process() " works
158 'foobar'->Process() " does NOT work
159 ('foobar')->Process() " works
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100161In case there is ambiguity between a function name and an Ex command, use ":"
162to make clear you want to use the Ex command. For example, there is both the
163`:substitute` command and the `substitute()` function. When the line starts
164with `substitute(` this will use the function, prepend a colon to use the
165command instead: >
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100166 :substitute(pattern (replacement (
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100167
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100168Note that while variables need to be defined before they can be used,
169functions can be called before being defined. This is required to be able
170have cyclic dependencies between functions. It is slightly less efficient,
171since the function has to be looked up by name. And a typo in the function
172name will only be found when the call is executed.
173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174
Bram Moolenaard1caa942020-04-10 22:10:56 +0200175Omitting function() ~
176
177A user defined function can be used as a function reference in an expression
178without `function()`. The argument types and return type will then be checked.
179The function must already have been defined. >
180
181 let Funcref = MyFunction
182
183When using `function()` the resulting type is "func", a function with any
184number of arguments and any return type. The function can be defined later.
185
186
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200187Automatic line continuation ~
188
189In many cases it is obvious that an expression continues on the next line. In
190those cases there is no need to prefix the line with a backslash. For
191example, when a list spans multiple lines: >
192 let mylist = [
193 'one',
194 'two',
195 ]
Bram Moolenaare6085c52020-04-12 20:19:16 +0200196And when a dict spans multiple lines: >
197 let mydict = #{
198 one: 1,
199 two: 2,
200 }
201Function call: >
202 let result = Func(
203 arg1,
204 arg2
205 )
206
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200207For binary operators iin expressions not in [], {} or () a line break is
208possible AFTER the operators. For example: >
209 let text = lead ..
210 middle ..
211 end
212 let total = start +
213 end -
214 correction
215 let result = positive ?
216 PosFunc(arg) :
217 NegFunc(arg)
218
Bram Moolenaare6085c52020-04-12 20:19:16 +0200219Note that "enddef" cannot be used at the start of a continuation line, it ends
220the current function.
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200221
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200222It is also possible to split a function header over multiple lines, in between
223arguments: >
224 def MyFunc(
225 text: string,
226 separator = '-'
227 ): string
228
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230No curly braces expansion ~
231
232|curly-braces-names| cannot be used.
233
234
Bram Moolenaar560979e2020-02-04 22:53:05 +0100235No :append, :change or :insert ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236
Bram Moolenaar560979e2020-02-04 22:53:05 +0100237These commands are too quickly confused with local variable names.
238
239
240Comparators ~
241
242The 'ignorecase' option is not used for comparators that use strings.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243
244
245White space ~
246
247Vim9 script enforces proper use of white space. This is no longer allowed: >
248 let var=234 " Error!
249 let var= 234 " Error!
250 let var =234 " Error!
251There must be white space before and after the "=": >
252 let var = 234 " OK
Bram Moolenaar2c330432020-04-13 14:41:35 +0200253White space must also be put before the # that starts a comment: >
254 let var = 234# Error!
255 let var = 234 # OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256
257White space is required around most operators.
258
259White space is not allowed:
260- Between a function name and the "(": >
261 call Func (arg) " Error!
262 call Func
263 \ (arg) " Error!
264 call Func(arg) " OK
265 call Func(
266 \ arg) " OK
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100267 call Func(
268 \ arg " OK
269 \ )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270
271
272Conditions and expressions ~
273
274Conditions and expression are mostly working like they do in JavaScript. A
275difference is made where JavaScript does not work like most people expect.
276Specifically, an empty list is falsey.
277
278Any type of variable can be used as a condition, there is no error, not even
279for using a list or job. This is very much like JavaScript, but there are a
280few exceptions.
281
282 type TRUE when ~
283 bool v:true
284 number non-zero
285 float non-zero
286 string non-empty
287 blob non-empty
288 list non-empty (different from JavaScript)
289 dictionary non-empty (different from JavaScript)
Bram Moolenaard1caa942020-04-10 22:10:56 +0200290 func when there is a function name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100291 special v:true
292 job when not NULL
293 channel when not NULL
294 class when not NULL
295 object when not NULL (TODO: when isTrue() returns v:true)
296
297The boolean operators "||" and "&&" do not change the value: >
298 8 || 2 == 8
299 0 || 2 == 2
300 0 || '' == ''
301 8 && 2 == 2
302 0 && 2 == 0
303 [] && 2 == []
304
305When using `..` for string concatenation the arguments are always converted to
306string. >
307 'hello ' .. 123 == 'hello 123'
308 'hello ' .. v:true == 'hello true'
309
310In Vim9 script one can use "true" for v:true and "false" for v:false.
311
312
313==============================================================================
314
3153. New style functions *fast-functions*
316
317THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
318
319 *:def*
320:def[!] {name}([arguments])[: {return-type}
321 Define a new function by the name {name}. The body of
322 the function follows in the next lines, until the
323 matching `:enddef`.
324
Bram Moolenaard77a8522020-04-03 21:59:57 +0200325 When {return-type} is omitted or is "void" the
326 function is not expected to return anything.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100327
328 {arguments} is a sequence of zero or more argument
329 declarations. There are three forms:
330 {name}: {type}
331 {name} = {value}
332 {name}: {type} = {value}
333 The first form is a mandatory argument, the caller
334 must always provide them.
335 The second and third form are optional arguments.
336 When the caller omits an argument the {value} is used.
337
Bram Moolenaar560979e2020-02-04 22:53:05 +0100338 NOTE: It is possible to nest `:def` inside another
339 `:def`, but it is not possible to nest `:def` inside
340 `:function`, for backwards compatibility.
341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342 [!] is used as with `:function`.
343
344 *:enddef*
345:enddef End of a function defined with `:def`.
346
347
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100348If the script the function is defined in is Vim9 script, then script-local
349variables can be accessed without the "s:" prefix. They must be defined
350before the function. If the script the function is defined in is legacy
351script, then script-local variables must be accessed with the "s:" prefix.
352
353
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100354 *:disa* *:disassemble*
355:disa[ssemble] {func} Show the instructions generated for {func}.
356 This is for debugging and testing.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100357 Note that for command line completion of {func} you
358 can prepend "s:" to find script-local functions.
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360==============================================================================
361
3624. Types *vim9-types*
363
364THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
365
366The following builtin types are supported:
367 bool
368 number
369 float
370 string
371 blob
Bram Moolenaard77a8522020-04-03 21:59:57 +0200372 list<{type}>
373 dict<{type}>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100374 job
375 channel
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100376 func
Bram Moolenaard1caa942020-04-10 22:10:56 +0200377 func: {type}
Bram Moolenaard77a8522020-04-03 21:59:57 +0200378 func({type}, ...)
379 func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380
381Not supported yet:
Bram Moolenaard77a8522020-04-03 21:59:57 +0200382 tuple<a: {type}, b: {type}, ...>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383
Bram Moolenaard77a8522020-04-03 21:59:57 +0200384These types can be used in declarations, but no value will have this type:
385 {type}|{type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100386 void
387 any
388
Bram Moolenaard77a8522020-04-03 21:59:57 +0200389There is no array type, use list<{type}> instead. For a list constant an
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390efficient implementation is used that avoids allocating lot of small pieces of
391memory.
392
Bram Moolenaard77a8522020-04-03 21:59:57 +0200393A partial and function can be declared in more or less specific ways:
394func any kind of function reference, no type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200395 checking for arguments or return value
Bram Moolenaard77a8522020-04-03 21:59:57 +0200396func: {type} any number and type of arguments with specific
397 return type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200398func({type}) function with argument type, does not return
Bram Moolenaard77a8522020-04-03 21:59:57 +0200399 a value
Bram Moolenaard1caa942020-04-10 22:10:56 +0200400func({type}): {type} function with argument type and return type
401func(?{type}) function with type of optional argument, does
402 not return a value
403func(...{type}) function with type of variable number of
404 arguments, does not return a value
405func({type}, ?{type}, ...{type}): {type}
406 function with:
407 - type of mandatory argument
408 - type of optional argument
409 - type of variable number of arguments
410 - return type
Bram Moolenaard77a8522020-04-03 21:59:57 +0200411
412If the return type is "void" the function does not return a value.
413
414The reference can also be a |Partial|, in which case it stores extra arguments
415and/or a dictionary, which are not visible to the caller. Since they are
416called in the same way the declaration is the same.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100417
418Custom types can be defined with `:type`: >
419 :type MyList list<string>
420{not implemented yet}
421
422And classes and interfaces can be used as types: >
423 :class MyClass
424 :let mine: MyClass
425
426 :interface MyInterface
427 :let mine: MyInterface
428
429 :class MyTemplate<Targ>
430 :let mine: MyTemplate<number>
431 :let mine: MyTemplate<string>
432
433 :class MyInterface<Targ>
434 :let mine: MyInterface<number>
435 :let mine: MyInterface<string>
436{not implemented yet}
437
438
439Type inference *type-inference*
440
441In general: Whenever the type is clear it can be omitted. For example, when
442declaring a variable and giving it a value: >
443 let var = 0 " infers number type
444 let var = 'hello' " infers string type
445
446
447==============================================================================
448
4495. Namespace, Import and Export
450 *vim9script* *vim9-export* *vim9-import*
451
452THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
453
454A Vim9 script can be written to be imported. This means that everything in
455the script is local, unless exported. Those exported items, and only those
456items, can then be imported in another script.
457
458
459Namespace ~
460 *:vim9script* *:vim9*
Bram Moolenaar560979e2020-02-04 22:53:05 +0100461To recognize a file that can be imported the `vim9script` statement must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462appear as the first statement in the file. It tells Vim to interpret the
463script in its own namespace, instead of the global namespace. If a file
464starts with: >
465 vim9script
466 let myvar = 'yes'
467Then "myvar" will only exist in this file. While without `vim9script` it would
468be available as `g:myvar` from any other script and function.
469
470The variables at the file level are very much like the script-local "s:"
471variables in legacy Vim script, but the "s:" is omitted.
472
473In Vim9 script the global "g:" namespace can still be used as before.
474
475A side effect of `:vim9script` is that the 'cpoptions' option is set to the
476Vim default value, like with: >
477 :set cpo&vim
478One of the effects is that |line-continuation| is always enabled.
479The original value of 'cpoptions' is restored at the end of the script.
480
481
482Export ~
483 *:export* *:exp*
484Exporting one item can be written as: >
485 export const EXPORTED_CONST = 1234
486 export let someValue = ...
487 export def MyFunc() ...
488 export class MyClass ...
489
490As this suggests, only constants, variables, `:def` functions and classes can
491be exported.
492
493Alternatively, an export statement can be used to export several already
494defined (otherwise script-local) items: >
495 export {EXPORTED_CONST, someValue, MyFunc, MyClass}
496
497
498Import ~
499 *:import* *:imp*
500The exported items can be imported individually in another Vim9 script: >
501 import EXPORTED_CONST from "thatscript.vim"
502 import MyClass from "myclass.vim"
503
504To import multiple items at the same time: >
505 import {someValue, MyClass} from "thatscript.vim"
506
Bram Moolenaar560979e2020-02-04 22:53:05 +0100507In case the name is ambiguous, another name can be specified: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 import MyClass as ThatClass from "myclass.vim"
509 import {someValue, MyClass as ThatClass} from "myclass.vim"
510
511To import all exported items under a specific identifier: >
512 import * as That from 'thatscript.vim'
513
514Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free
515to choose the name "That", but it is highly recommended to use the name of the
516script file to avoid confusion.
517
518The script name after `import` can be:
519- A relative path, starting "." or "..". This finds a file relative to the
520 location of the script file itself. This is useful to split up a large
521 plugin into several files.
522- An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This
523 will be rarely used.
524- A path not being relative or absolute. This will be found in the
525 "import" subdirectories of 'runtimepath' entries. The name will usually be
526 longer and unique, to avoid loading the wrong file.
527
528Once a vim9 script file has been imported, the result is cached and used the
529next time the same script is imported. It will not be read again.
530 *:import-cycle*
531The `import` commands are executed when encountered. If that script (directly
532or indirectly) imports the current script, then items defined after the
533`import` won't be processed yet. Therefore cyclic imports can exist, but may
534result in undefined items.
535
536
537Import in an autoload script ~
538
539For optimal startup speed, loading scripts should be postponed until they are
Bram Moolenaar560979e2020-02-04 22:53:05 +0100540actually needed. A recommended mechanism:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541
5421. In the plugin define user commands, functions and/or mappings that refer to
543 an autoload script. >
544 command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>)
545
546< This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen.
547
5482. In the autocommand script do the actual work. You can import items from
549 other files to split up functionality in appropriate pieces. >
550 vim9script
551 import FilterFunc from "../import/someother.vim"
552 def searchfor#Stuff(arg: string)
553 let filtered = FilterFunc(arg)
554 ...
555< This goes in .../autoload/searchfor.vim. "searchfor" in the file name
556 must be exactly the same as the prefix for the function name, that is how
557 Vim finds the file.
558
5593. Other functionality, possibly shared between plugins, contains the exported
560 items and any private items. >
561 vim9script
562 let localVar = 'local'
563 export def FilterFunc(arg: string): string
564 ...
565< This goes in .../import/someother.vim.
566
567
568Import in legacy Vim script ~
569
570If an `import` statement is used in legacy Vim script, for identifier the
571script-local "s:" namespace will be used, even when "s:" is not specified.
572
573
574==============================================================================
575
5769. Rationale *vim9-rationale*
577
578The :def command ~
579
580Plugin writers have asked for a much faster Vim script. Investigation have
Bram Moolenaar560979e2020-02-04 22:53:05 +0100581shown that keeping the existing semantics of function calls make this close to
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582impossible, because of the overhead involved with calling a function, setting
583up the local function scope and executing lines. There are many details that
584need to be handled, such as error messages and exceptions. The need to create
585a dictionary for a: and l: scopes, the a:000 list and several others add too
586much overhead that cannot be avoided.
587
588Therefore the `:def` method to define a new-style function had to be added,
589which allows for a function with different semantics. Most things still work
590as before, but some parts do not. A new way to define a function was
591considered the best way to separate the old-style code from Vim9 script code.
592
593Using "def" to define a function comes from Python. Other languages use
594"function" which clashes with legacy Vim script.
595
596
597Type checking ~
598
599When compiling lines of Vim commands into instructions as much as possible
600should be done at compile time. Postponing it to runtime makes the execution
601slower and means mistakes are found only later. For example, when
602encountering the "+" character and compiling this into a generic add
603instruction, at execution time the instruction would have to inspect the type
604of the arguments and decide what kind of addition to do. And when the
605type is dictionary throw an error. If the types are known to be numbers then
606an "add number" instruction can be used, which is faster. The error can be
607given at compile time, no error handling is needed at runtime.
608
609The syntax for types is similar to Java, since it is easy to understand and
610widely used. The type names are what was used in Vim before, with some
611additions such as "void" and "bool".
612
613
614JavaScript/TypeScript syntax and semantics ~
615
616Script writers have complained that the Vim script syntax is unexpectedly
617different from what they are used to. To reduce this complaint popular
618languages will be used as an example. At the same time, we do not want to
Bram Moolenaar560979e2020-02-04 22:53:05 +0100619abandon the well-known parts of legacy Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620
621Since Vim already uses `:let` and `:const` and optional type checking is
622desirable, the JavaScript/TypeScript syntax fits best for variable
623declarations. >
624 const greeting = 'hello' " string type is inferred
625 let name: string
626 ...
627 name = 'John'
628
629Expression evaluation was already close to what JavaScript and other languages
630are doing. Some details are unexpected and can be fixed. For example how the
631|| and && operators work. Legacy Vim script: >
632 let result = 44
633 ...
634 return result || 0 " returns 1
635
636Vim9 script works like JavaScript, keep the value: >
637 let result = 44
638 ...
639 return result || 0 " returns 44
640
641On the other hand, overloading "+" to use both for addition and string
642concatenation goes against legacy Vim script and often leads to mistakes.
643For that reason we will keep using ".." for string concatenation. Lua also
644uses ".." this way.
645
646
647Import and Export ~
648
649A problem of legacy Vim script is that by default all functions and variables
650are global. It is possible to make them script-local, but then they are not
651available in other scripts.
652
653In Vim9 script a mechanism very similar to the Javascript import and export
654mechanism is supported. It is a variant to the existing `:source` command
655that works like one would expect:
656- Instead of making everything global by default, everything is script-local,
657 unless exported.
658- When importing a script the symbols that are imported are listed, avoiding
659 name conflicts and failures if later functionality is added.
660- The mechanism allows for writing a big, long script with a very clear API:
661 the exported function(s) and class(es).
662- By using relative paths loading can be much faster for an import inside of a
663 package, no need to search many directories.
664- Once an import has been used, it can be cached and loading it again can be
665 avoided.
666- The Vim-specific use of "s:" to make things script-local can be dropped.
667
668
669Classes ~
670
671Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But
672these have never become widespread. When Vim 9 was designed a decision was
673made to phase out these interfaces and concentrate on Vim script, while
674encouraging plugin authors to write code in any language and run it as an
675external tool, using jobs and channels.
676
677Still, using an external tool has disadvantages. An alternative is to convert
678the tool into Vim script. For that to be possible without too much
679translation, and keeping the code fast at the same time, the constructs of the
680tool need to be supported. Since most languages support classes the lack of
681class support in Vim is then a problem.
682
683Previously Vim supported a kind-of object oriented programming by adding
684methods to a dictionary. With some care this could be made to work, but it
685does not look like real classes. On top of that, it's very slow, because of
686the use of dictionaries.
687
688The support of classes in Vim9 script is a "minimal common functionality" of
689class support in most languages. It works mostly like Java, which is the most
690popular programming language.
691
692
693
694 vim:tw=78:ts=8:noet:ft=help:norl: