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