blob: 8eb60be26391aaf2d2366b269763c4e7cb90be5c [file] [log] [blame]
Bram Moolenaar207f0092020-08-30 17:20:20 +02001*vim9.txt* For Vim version 8.2. Last change: 2020 Aug 27
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
Bram Moolenaar7ceefb32020-05-01 16:07:38 +02009Vim9 script commands and expressions. *vim9*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010
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
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200171. What is Vim9 script? |vim9-script|
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182. 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
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020031Vim script has been growing over time, while preserving backwards
32compatibility. That means bad choices from the past often can't be changed
Bram Moolenaar73fef332020-06-21 22:12:03 +020033and compatibility with Vi restricts possible solutions. Execution is quite
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020034slow, each line is parsed every time it is executed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010035
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020036The main goal of Vim9 script is to drastically improve performance. This is
37accomplished by compiling commands into instructions that can be efficiently
38executed. An increase in execution speed of 10 to 100 times can be expected.
39
40A secondary goal is to avoid Vim-specific constructs and get closer to
41commonly used programming languages, such as JavaScript, TypeScript and Java.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042
43The performance improvements can only be achieved by not being 100% backwards
Bram Moolenaar388a5d42020-05-26 21:20:45 +020044compatible. For example, making function arguments available in the
45"a:" dictionary adds quite a lot of overhead. In a Vim9 function this
46dictionary is not available. Other differences are more subtle, such as how
47errors are handled.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048
49The Vim9 script syntax and semantics are used in:
50- a function defined with the `:def` command
51- a script file where the first command is `vim9script`
Bram Moolenaar207f0092020-08-30 17:20:20 +020052- an autocommand defined in the context of these
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010053
54When using `:function` in a Vim9 script file the legacy syntax is used.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020055However, this can be confusing and is therefore discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020057Vim9 script and legacy Vim script can be mixed. There is no requirement to
58rewrite old scripts, they keep working as before.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059
60==============================================================================
61
622. Differences from legacy Vim script *vim9-differences*
63
64THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
65
Bram Moolenaar2c330432020-04-13 14:41:35 +020066Comments starting with # ~
67
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020068In legacy Vim script comments start with double quote. In Vim9 script
69comments start with #. >
70 # declarations
Bram Moolenaar73fef332020-06-21 22:12:03 +020071 let count = 0 # number of occurrences
Bram Moolenaar2c330432020-04-13 14:41:35 +020072
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020073The reason is that a double quote can also be the start of a string. In many
Bram Moolenaar3d1cde82020-08-15 18:55:18 +020074places, especially halfway through an expression with a line break, it's hard
75to tell what the meaning is, since both a string and a comment can be followed
76by arbitrary text. To avoid confusion only # comments are recognized. This
77is the same as in shell scripts and Python programs.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020078
79In Vi # is a command to list text with numbers. In Vim9 script you can use
80`:number` for that. >
Bram Moolenaarae616492020-07-28 20:07:27 +020081 101 number
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020082
83To improve readability there must be a space between a command and the #
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020084that starts a comment. Note that #{ is the start of a dictionary, therefore
Bram Moolenaarae616492020-07-28 20:07:27 +020085it does not start a comment.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020086
Bram Moolenaar2c330432020-04-13 14:41:35 +020087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088Vim9 functions ~
89
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020090A function defined with `:def` is compiled. Execution is many times faster,
91often 10x to 100x times.
92
Bram Moolenaar388a5d42020-05-26 21:20:45 +020093Many errors are already found when compiling, before the function is executed.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020094The syntax is strict, to enforce code that is easy to read and understand.
95
Bram Moolenaar207f0092020-08-30 17:20:20 +020096Compilation is done when:
97- the function is first called
98- when the `:defcompile` command is encountered in the script where the
99 function was defined
100- `:disassemble` is used for the function.
101- a function that is compiled calls the function or uses it as a function
102 reference
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200103
104`:def` has no options like `:function` does: "range", "abort", "dict" or
105"closure". A `:def` function always aborts on an error, does not get a range
106passed and cannot be a "dict" function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200108The argument types and return type need to be specified. The "any" type can
109be used, type checking will then be done at runtime, like with legacy
110functions.
111
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200112Arguments are accessed by name, without "a:", just like any other language.
113There is no "a:" dictionary or "a:000" list.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200114
115Variable arguments are defined as the last argument, with a name and have a
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200116list type, similar to TypeScript. For example, a list of numbers: >
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200117 def MyFunc(...itemlist: list<number>)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100118 for item in itemlist
119 ...
120
121
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200122Functions and variables are script-local by default ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200123 *vim9-scopes*
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200124When using `:function` or `:def` to specify a new function at the script level
125in a Vim9 script, the function is local to the script, as if "s:" was
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200126prefixed. Using the "s:" prefix is optional. To define or use a global
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200127function or variable the "g:" prefix should be used. For functions in an
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200128autoload script the "name#" prefix is sufficient. >
129 def ThisFunction() # script-local
130 def s:ThisFunction() # script-local
131 def g:ThatFunction() # global
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200132 def ThatFunction() # global if no local ThatFunction()
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200133 def scriptname#function() # autoload
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200134
135When using `:function` or `:def` to specify a new function inside a function,
136the function is local to the function. It is not possible to define a
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200137script-local function inside a function. It is possible to define a global
138function, using the "g:" prefix.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200139
140When referring to a function and no "s:" or "g:" prefix is used, Vim will
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200141prefer using a local function (in the function scope, script scope or
142imported) before looking for a global function.
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200143In all cases the function must be defined before used. That is when it is
144first called or when `:defcompile` causes the call to be compiled.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200145
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200146The result is that functions and variables without a namespace can usually be
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200147found in the script, either defined there or imported. Global functions and
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200148variables could be defined anywhere (good luck finding out where!).
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200149
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200150Global functions can still be defined and deleted at nearly any time. In
Bram Moolenaar2cfb4a22020-05-07 18:56:00 +0200151Vim9 script script-local functions are defined once when the script is sourced
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200152and cannot be deleted or replaced.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200153
154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155Variable declarations with :let and :const ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200156 *vim9-declaration*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157Local variables need to be declared with `:let`. Local constants need to be
158declared with `:const`. We refer to both as "variables".
159
160Variables can be local to a script, function or code block: >
161 vim9script
162 let script_var = 123
163 def SomeFunc()
164 let func_var = script_var
165 if cond
166 let block_var = func_var
167 ...
168
169The variables are only visible in the block where they are defined and nested
170blocks. Once the block ends the variable is no longer accessible: >
171 if cond
172 let inner = 5
173 else
174 let inner = 0
175 endif
176 echo inner " Error!
177
178The declaration must be done earlier: >
179 let inner: number
180 if cond
181 inner = 5
182 else
183 inner = 0
184 endif
185 echo inner
186
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200187To intentionally avoid a variable being available later, a block can be used:
188>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189 {
190 let temp = 'temp'
191 ...
192 }
193 echo temp " Error!
194
Bram Moolenaar560979e2020-02-04 22:53:05 +0100195An existing variable cannot be assigned to with `:let`, since that implies a
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200196declaration. Global, window, tab, buffer and Vim variables can only be used
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200197without `:let`, because they are not really declared, they can also be deleted
198with `:unlet`.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200200Variables and functions cannot shadow previously defined or imported variables
201and functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202Variables may shadow Ex commands, rename the variable if needed.
203
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200204Global variables and user defined functions must be prefixed with "g:", also
205at the script level. >
Bram Moolenaard1caa942020-04-10 22:10:56 +0200206 vim9script
207 let script_local = 'text'
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200208 g:global = 'value'
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200209 let Funcref = g:ThatFunction
Bram Moolenaard1caa942020-04-10 22:10:56 +0200210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be
212used to repeat a `:substitute` command.
213
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200214 *E1092*
215Declaring more than one variable at a time, using the unpack notation, is
216currently not supported: >
217 let [v1, v2] = GetValues() # Error!
218That is because the type needs to be inferred from the list item type, which
219isn't that easy.
220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221
222Omitting :call and :eval ~
223
224Functions can be called without `:call`: >
225 writefile(lines, 'file')
Bram Moolenaar560979e2020-02-04 22:53:05 +0100226Using `:call` is still possible, but this is discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227
228A method call without `eval` is possible, so long as the start is an
Bram Moolenaarae616492020-07-28 20:07:27 +0200229identifier or can't be an Ex command. Examples: >
230 myList->add(123)
231 g:myList->add(123)
232 [1, 2, 3]->Process()
233 #{a: 1, b: 2}->Process()
234 {'a': 1, 'b': 2}->Process()
235 "foobar"->Process()
236 ("foobar")->Process()
237 'foobar'->Process()
238 ('foobar')->Process()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200240In the rare case there is ambiguity between a function name and an Ex command,
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200241prepend ":" to make clear you want to use the Ex command. For example, there
242is both the `:substitute` command and the `substitute()` function. When the
243line starts with `substitute(` this will use the function. Prepend a colon to
244use the command instead: >
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100245 :substitute(pattern (replacement (
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100246
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100247Note that while variables need to be defined before they can be used,
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200248functions can be called before being defined. This is required to allow
249for cyclic dependencies between functions. It is slightly less efficient,
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100250since the function has to be looked up by name. And a typo in the function
Bram Moolenaarae616492020-07-28 20:07:27 +0200251name will only be found when the function is called.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
Bram Moolenaard1caa942020-04-10 22:10:56 +0200254Omitting function() ~
255
256A user defined function can be used as a function reference in an expression
257without `function()`. The argument types and return type will then be checked.
258The function must already have been defined. >
259
260 let Funcref = MyFunction
261
262When using `function()` the resulting type is "func", a function with any
263number of arguments and any return type. The function can be defined later.
264
265
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200266Automatic line continuation ~
267
268In many cases it is obvious that an expression continues on the next line. In
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200269those cases there is no need to prefix the line with a backslash
270|line-continuation|. For example, when a list spans multiple lines: >
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200271 let mylist = [
272 'one',
273 'two',
274 ]
Bram Moolenaare6085c52020-04-12 20:19:16 +0200275And when a dict spans multiple lines: >
276 let mydict = #{
277 one: 1,
278 two: 2,
279 }
280Function call: >
281 let result = Func(
282 arg1,
283 arg2
284 )
285
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200286For binary operators in expressions not in [], {} or () a line break is
287possible just before or after the operator. For example: >
288 let text = lead
289 .. middle
290 .. end
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200291 let total = start +
292 end -
293 correction
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200294 let result = positive
295 ? PosFunc(arg)
296 : NegFunc(arg)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200297
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200298For a method call using "->" and a member using a dot, a line break is allowed
299before it: >
Bram Moolenaar73fef332020-06-21 22:12:03 +0200300 let result = GetBuilder()
301 ->BuilderSetWidth(333)
302 ->BuilderSetHeight(777)
303 ->BuilderBuild()
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200304 let result = MyDict
305 .member
Bram Moolenaar73fef332020-06-21 22:12:03 +0200306
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200307< *E1050*
308To make it possible for the operator at the start of the line to be
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200309recognized, it is required to put a colon before a range. This will add
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200310"start" and print: >
311 let result = start
312 + print
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200313Like this: >
314 let result = start + print
315
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200316This will assign "start" and print a line: >
317 let result = start
318 :+ print
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200319
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200320It is also possible to split a function header over multiple lines, in between
321arguments: >
322 def MyFunc(
323 text: string,
324 separator = '-'
325 ): string
326
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200327Notes:
328- "enddef" cannot be used at the start of a continuation line, it ends the
329 current function.
330- No line break is allowed in the LHS of an assignment. Specifically when
331 unpacking a list |:let-unpack|. This is OK: >
332 [var1, var2] =
333 Func()
334< This does not work: >
335 [var1,
336 var2] =
337 Func()
338- No line break is allowed in between arguments of an `:echo`, `:execute` and
339 similar commands. This is OK: >
340 echo [1,
341 2] [3,
342 4]
343< This does not work: >
344 echo [1, 2]
345 [3, 4]
346- No line break is allowed in the arguments of a lambda, between the "{" and
347 "->". This is OK: >
348 filter(list, {k, v ->
349 v > 0})
350< This does not work: >
351 filter(list, {k,
352 v -> v > 0})
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200353
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355No curly braces expansion ~
356
357|curly-braces-names| cannot be used.
358
359
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200360No :xit, :t, :append, :change or :insert ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100361
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200362These commands are too easily confused with local variable names.
363Instead of `:x` or `:xit` you can use `:exit`.
364Instead of `:t` you can use `:copy`.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100365
366
367Comparators ~
368
369The 'ignorecase' option is not used for comparators that use strings.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370
371
372White space ~
373
374Vim9 script enforces proper use of white space. This is no longer allowed: >
375 let var=234 " Error!
376 let var= 234 " Error!
377 let var =234 " Error!
378There must be white space before and after the "=": >
379 let var = 234 " OK
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200380White space must also be put before the # that starts a comment after a
381command: >
Bram Moolenaar2c330432020-04-13 14:41:35 +0200382 let var = 234# Error!
383 let var = 234 # OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384
385White space is required around most operators.
386
387White space is not allowed:
388- Between a function name and the "(": >
389 call Func (arg) " Error!
390 call Func
391 \ (arg) " Error!
392 call Func(arg) " OK
393 call Func(
394 \ arg) " OK
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100395 call Func(
396 \ arg " OK
397 \ )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398
399
400Conditions and expressions ~
401
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200402Conditions and expressions are mostly working like they do in JavaScript. A
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403difference is made where JavaScript does not work like most people expect.
404Specifically, an empty list is falsey.
405
406Any type of variable can be used as a condition, there is no error, not even
407for using a list or job. This is very much like JavaScript, but there are a
408few exceptions.
409
410 type TRUE when ~
411 bool v:true
412 number non-zero
413 float non-zero
414 string non-empty
415 blob non-empty
416 list non-empty (different from JavaScript)
417 dictionary non-empty (different from JavaScript)
Bram Moolenaard1caa942020-04-10 22:10:56 +0200418 func when there is a function name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 special v:true
420 job when not NULL
421 channel when not NULL
422 class when not NULL
423 object when not NULL (TODO: when isTrue() returns v:true)
424
425The boolean operators "||" and "&&" do not change the value: >
426 8 || 2 == 8
427 0 || 2 == 2
428 0 || '' == ''
429 8 && 2 == 2
430 0 && 2 == 0
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200431 2 && 0 == 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 [] && 2 == []
433
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200434When using `..` for string concatenation arguments of simple types are always
435converted to string. >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436 'hello ' .. 123 == 'hello 123'
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200437 'hello ' .. v:true == 'hello v:true'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100438
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200439Simple types are string, float, special and bool. For other types |string()|
440can be used.
441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442In Vim9 script one can use "true" for v:true and "false" for v:false.
443
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200444Indexing a string with [idx] or [idx, idx] uses character indexes instead of
445byte indexes. Example: >
446 echo 'bár'[1]
447In legacy script this results in the character 0xc3 (an illegal byte), in Vim9
448script this results in the string 'á'.
449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450
Bram Moolenaare46a4402020-06-30 20:38:27 +0200451What to watch out for ~
452 *vim9-gotchas*
453Vim9 was designed to be closer to often used programming languages, but at the
454same time tries to support the legacy Vim commands. Some compromises had to
455be made. Here is a summary of what might be unexpected.
456
457Ex command ranges need to be prefixed with a colon. >
458 -> " legacy Vim: shifts the previous line to the right
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200459 ->func() " Vim9: method call in continuation line
Bram Moolenaare46a4402020-06-30 20:38:27 +0200460 :-> " Vim9: shifts the previous line to the right
461
462 %s/a/b " legacy Vim: substitute on all lines
463 x = alongname
464 % another " Vim9: line continuation without a backslash
465 :%s/a/b " Vim9: substitute on all lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200466 'text'->func() " Vim9: method call
467 :'t " legacy Vim: jump to mark m
Bram Moolenaare46a4402020-06-30 20:38:27 +0200468
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200469Some Ex commands can be confused with assignments in Vim9 script: >
470 g:name = value # assignment
471 g:pattern:cmd # invalid command - ERROR
472 :g:pattern:cmd # :global command
473
Bram Moolenaare46a4402020-06-30 20:38:27 +0200474Functions defined with `:def` compile the whole function. Legacy functions
475can bail out, and the following lines are not parsed: >
476 func Maybe()
477 if !has('feature')
478 return
479 endif
480 use-feature
481 endfunc
482Vim9 functions are compiled as a whole: >
483 def Maybe()
484 if !has('feature')
485 return
486 endif
487 use-feature " May give compilation error
488 enddef
489For a workaround, split it in two functions: >
490 func Maybe()
491 if has('feature')
492 call MaybyInner()
493 endif
494 endfunc
495 if has('feature')
496 def MaybeInner()
497 use-feature
498 enddef
499 endif
Bram Moolenaar207f0092020-08-30 17:20:20 +0200500Of put the unsupported code inside an `if` with a constant expression that
501evaluates to false: >
502 def Maybe()
503 if has('feature')
504 use-feature
505 endif
506 enddef
507Note that for unrecognized commands there is no check for "|" and a following
508command. This will give an error for missing `endif`: >
509 def Maybe()
510 if has('feature') | use-feature | endif
511 enddef
Bram Moolenaare46a4402020-06-30 20:38:27 +0200512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513==============================================================================
514
5153. New style functions *fast-functions*
516
517THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
518
519 *:def*
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200520:def[!] {name}([arguments])[: {return-type}]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 Define a new function by the name {name}. The body of
522 the function follows in the next lines, until the
523 matching `:enddef`.
524
Bram Moolenaard77a8522020-04-03 21:59:57 +0200525 When {return-type} is omitted or is "void" the
526 function is not expected to return anything.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527
528 {arguments} is a sequence of zero or more argument
529 declarations. There are three forms:
530 {name}: {type}
531 {name} = {value}
532 {name}: {type} = {value}
533 The first form is a mandatory argument, the caller
534 must always provide them.
535 The second and third form are optional arguments.
536 When the caller omits an argument the {value} is used.
537
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200538 The function will be compiled into instructions when
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200539 called, or when `:disassemble` or `:defcompile` is
540 used. Syntax and type errors will be produced at that
541 time.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200542
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200543 It is possible to nest `:def` inside another `:def` or
544 `:function` up to about 50 levels deep.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100545
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200546 [!] is used as with `:function`. Note that in Vim9
547 script script-local functions cannot be deleted or
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200548 redefined later in the same script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549
550 *:enddef*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200551:enddef End of a function defined with `:def`. It should be on
552 a line by its own.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553
554
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100555If the script the function is defined in is Vim9 script, then script-local
556variables can be accessed without the "s:" prefix. They must be defined
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200557before the function is compiled. If the script the function is defined in is
558legacy script, then script-local variables must be accessed with the "s:"
Bram Moolenaar207f0092020-08-30 17:20:20 +0200559prefix and they do not need to exist (they can be deleted any time).
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100560
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200561 *:defc* *:defcompile*
562:defc[ompile] Compile functions defined in the current script that
563 were not compiled yet.
564 This will report errors found during the compilation.
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100565
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100566 *:disa* *:disassemble*
567:disa[ssemble] {func} Show the instructions generated for {func}.
568 This is for debugging and testing.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100569 Note that for command line completion of {func} you
570 can prepend "s:" to find script-local functions.
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100571
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200572Limitations ~
573
574Local variables will not be visible to string evaluation. For example: >
575 def EvalString(): list<string>
576 let list = ['aa', 'bb', 'cc', 'dd']
577 return range(1, 2)->map('list[v:val]')
578 enddef
579
580The map argument is a string expression, which is evaluated without the
581function scope. Instead, use a lambda: >
582 def EvalString(): list<string>
583 let list = ['aa', 'bb', 'cc', 'dd']
584 return range(1, 2)->map({ _, v -> list[v] })
585 enddef
586
587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588==============================================================================
589
5904. Types *vim9-types*
591
592THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
593
594The following builtin types are supported:
595 bool
596 number
597 float
598 string
599 blob
Bram Moolenaard77a8522020-04-03 21:59:57 +0200600 list<{type}>
601 dict<{type}>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 job
603 channel
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100604 func
Bram Moolenaard1caa942020-04-10 22:10:56 +0200605 func: {type}
Bram Moolenaard77a8522020-04-03 21:59:57 +0200606 func({type}, ...)
607 func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
609Not supported yet:
Bram Moolenaard77a8522020-04-03 21:59:57 +0200610 tuple<a: {type}, b: {type}, ...>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611
Bram Moolenaard77a8522020-04-03 21:59:57 +0200612These types can be used in declarations, but no value will have this type:
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200613 {type}|{type} {not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 void
615 any
616
Bram Moolenaard77a8522020-04-03 21:59:57 +0200617There is no array type, use list<{type}> instead. For a list constant an
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618efficient implementation is used that avoids allocating lot of small pieces of
619memory.
620
Bram Moolenaard77a8522020-04-03 21:59:57 +0200621A partial and function can be declared in more or less specific ways:
622func any kind of function reference, no type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200623 checking for arguments or return value
Bram Moolenaard77a8522020-04-03 21:59:57 +0200624func: {type} any number and type of arguments with specific
625 return type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200626func({type}) function with argument type, does not return
Bram Moolenaard77a8522020-04-03 21:59:57 +0200627 a value
Bram Moolenaard1caa942020-04-10 22:10:56 +0200628func({type}): {type} function with argument type and return type
629func(?{type}) function with type of optional argument, does
630 not return a value
631func(...{type}) function with type of variable number of
632 arguments, does not return a value
633func({type}, ?{type}, ...{type}): {type}
634 function with:
635 - type of mandatory argument
636 - type of optional argument
637 - type of variable number of arguments
638 - return type
Bram Moolenaard77a8522020-04-03 21:59:57 +0200639
640If the return type is "void" the function does not return a value.
641
642The reference can also be a |Partial|, in which case it stores extra arguments
643and/or a dictionary, which are not visible to the caller. Since they are
644called in the same way the declaration is the same.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645
646Custom types can be defined with `:type`: >
647 :type MyList list<string>
Bram Moolenaar127542b2020-08-09 17:22:04 +0200648Custom types must start with a capital letter, to avoid name clashes with
649builtin types added later, similarly to user functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650{not implemented yet}
651
652And classes and interfaces can be used as types: >
653 :class MyClass
654 :let mine: MyClass
655
656 :interface MyInterface
657 :let mine: MyInterface
658
659 :class MyTemplate<Targ>
660 :let mine: MyTemplate<number>
661 :let mine: MyTemplate<string>
662
663 :class MyInterface<Targ>
664 :let mine: MyInterface<number>
665 :let mine: MyInterface<string>
666{not implemented yet}
667
668
Bram Moolenaar64d662d2020-08-09 19:02:50 +0200669Variable types and type casting *variable-types*
670
671Variables declared in Vim9 script or in a `:def` function have a type, either
672specified explicitly or inferred from the initialization.
673
674Global, buffer, window and tab page variables do not have a specific type, the
675value can be changed at any time, possibly changing the type. Therefore, in
676compiled code the "any" type is assumed.
677
678This can be a problem when the "any" type is undesired and the actual type is
679expected to always be the same. For example, when declaring a list: >
680 let l: list<number> = [1, g:two]
681This will give an error, because "g:two" has type "any". To avoid this, use a
682type cast: >
683 let l: list<number> = [1, <number>g:two]
684< *type-casting*
685The compiled code will then check that "g:two" is a number at runtime and give
686an error if it isn't. This is called type casting.
687
688The syntax of a type cast is: "<" {type} ">". There cannot be white space
689after the "<" or before the ">" (to avoid them being confused with
690smaller-than and bigger-than operators).
691
692The semantics is that, if needed, a runtime type check is performed. The
693value is not actually changed. If you need to change the type, e.g. to change
694it to a string, use the |string()| function. Or use |str2nr()| to convert a
695string to a number.
696
697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698Type inference *type-inference*
699
700In general: Whenever the type is clear it can be omitted. For example, when
701declaring a variable and giving it a value: >
702 let var = 0 " infers number type
703 let var = 'hello' " infers string type
704
Bram Moolenaar127542b2020-08-09 17:22:04 +0200705The type of a list and dictionary comes from the common type of the values.
706If the values all have the same type, that type is used for the list or
707dictionary. If there is a mix of types, the "any" type is used. >
708 [1, 2, 3] list<number>
709 ['a', 'b', 'c'] list<string>
710 [1, 'x', 3] list<any>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711
Bram Moolenaar207f0092020-08-30 17:20:20 +0200712
713Stricter type checking *type-checking*
714
715In legacy Vim script, where a number was expected, a string would be
716automatically converted to a number. This was convenient for an actual number
717such as "123", but leads to unexpected problems (but no error message) if the
718string doesn't start with a number. Quite often this leads to hard-to-find
719bugs.
720
721In Vim9 script this has been made stricter. In most places it works just as
722before, if the expected type was already. There will sometimes be an error,
723thus breaking backwards compatibility. For example:
724- Using a number other than 0 or 1 where a boolean is expected. *E1023*
725- Using a string value when setting a number options.
726- Using a number where a string is expected. *E1024*
727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728==============================================================================
729
7305. Namespace, Import and Export
731 *vim9script* *vim9-export* *vim9-import*
732
733THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
734
735A Vim9 script can be written to be imported. This means that everything in
736the script is local, unless exported. Those exported items, and only those
737items, can then be imported in another script.
738
Bram Moolenaar207f0092020-08-30 17:20:20 +0200739You can cheat by using the global namespace explicitly. We will assume here
740that you don't do that.
741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
743Namespace ~
744 *:vim9script* *:vim9*
Bram Moolenaar560979e2020-02-04 22:53:05 +0100745To recognize a file that can be imported the `vim9script` statement must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746appear as the first statement in the file. It tells Vim to interpret the
747script in its own namespace, instead of the global namespace. If a file
748starts with: >
749 vim9script
750 let myvar = 'yes'
751Then "myvar" will only exist in this file. While without `vim9script` it would
752be available as `g:myvar` from any other script and function.
753
754The variables at the file level are very much like the script-local "s:"
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200755variables in legacy Vim script, but the "s:" is omitted. And they cannot be
756deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200758In Vim9 script the global "g:" namespace can still be used as before. And the
759"w:", "b:" and "t:" namespaces. These have in common that variables are not
760declared and they can be deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761
762A side effect of `:vim9script` is that the 'cpoptions' option is set to the
763Vim default value, like with: >
764 :set cpo&vim
765One of the effects is that |line-continuation| is always enabled.
766The original value of 'cpoptions' is restored at the end of the script.
767
768
769Export ~
770 *:export* *:exp*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200771Exporting an item can be written as: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 export const EXPORTED_CONST = 1234
773 export let someValue = ...
774 export def MyFunc() ...
775 export class MyClass ...
776
777As this suggests, only constants, variables, `:def` functions and classes can
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200778be exported. {classes are not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200780 *E1042*
781`:export` can only be used in Vim9 script, at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782
783
784Import ~
Bram Moolenaar73fef332020-06-21 22:12:03 +0200785 *:import* *:imp* *E1094*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786The exported items can be imported individually in another Vim9 script: >
787 import EXPORTED_CONST from "thatscript.vim"
788 import MyClass from "myclass.vim"
789
790To import multiple items at the same time: >
791 import {someValue, MyClass} from "thatscript.vim"
792
Bram Moolenaar560979e2020-02-04 22:53:05 +0100793In case the name is ambiguous, another name can be specified: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 import MyClass as ThatClass from "myclass.vim"
795 import {someValue, MyClass as ThatClass} from "myclass.vim"
796
797To import all exported items under a specific identifier: >
798 import * as That from 'thatscript.vim'
799
800Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free
801to choose the name "That", but it is highly recommended to use the name of the
802script file to avoid confusion.
803
804The script name after `import` can be:
805- A relative path, starting "." or "..". This finds a file relative to the
806 location of the script file itself. This is useful to split up a large
807 plugin into several files.
808- An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This
809 will be rarely used.
810- A path not being relative or absolute. This will be found in the
811 "import" subdirectories of 'runtimepath' entries. The name will usually be
812 longer and unique, to avoid loading the wrong file.
813
814Once a vim9 script file has been imported, the result is cached and used the
815next time the same script is imported. It will not be read again.
816 *:import-cycle*
817The `import` commands are executed when encountered. If that script (directly
818or indirectly) imports the current script, then items defined after the
819`import` won't be processed yet. Therefore cyclic imports can exist, but may
820result in undefined items.
821
822
823Import in an autoload script ~
824
825For optimal startup speed, loading scripts should be postponed until they are
Bram Moolenaar560979e2020-02-04 22:53:05 +0100826actually needed. A recommended mechanism:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827
8281. In the plugin define user commands, functions and/or mappings that refer to
829 an autoload script. >
830 command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>)
831
832< This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen.
833
Bram Moolenaar3d1cde82020-08-15 18:55:18 +02008342. In the autoload script do the actual work. You can import items from
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 other files to split up functionality in appropriate pieces. >
836 vim9script
837 import FilterFunc from "../import/someother.vim"
838 def searchfor#Stuff(arg: string)
839 let filtered = FilterFunc(arg)
840 ...
841< This goes in .../autoload/searchfor.vim. "searchfor" in the file name
842 must be exactly the same as the prefix for the function name, that is how
843 Vim finds the file.
844
8453. Other functionality, possibly shared between plugins, contains the exported
846 items and any private items. >
847 vim9script
848 let localVar = 'local'
849 export def FilterFunc(arg: string): string
850 ...
851< This goes in .../import/someother.vim.
852
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200853When compiling a `:def` function and a function in an autoload script is
854encountered, the script is not loaded until the `:def` function is called.
855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856
857Import in legacy Vim script ~
858
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200859If an `import` statement is used in legacy Vim script, the script-local "s:"
860namespace will be used for the imported item, even when "s:" is not specified.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861
862
863==============================================================================
864
8659. Rationale *vim9-rationale*
866
867The :def command ~
868
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200869Plugin writers have asked for a much faster Vim script. Investigations have
Bram Moolenaar560979e2020-02-04 22:53:05 +0100870shown that keeping the existing semantics of function calls make this close to
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871impossible, because of the overhead involved with calling a function, setting
872up the local function scope and executing lines. There are many details that
873need to be handled, such as error messages and exceptions. The need to create
874a dictionary for a: and l: scopes, the a:000 list and several others add too
875much overhead that cannot be avoided.
876
877Therefore the `:def` method to define a new-style function had to be added,
878which allows for a function with different semantics. Most things still work
879as before, but some parts do not. A new way to define a function was
880considered the best way to separate the old-style code from Vim9 script code.
881
882Using "def" to define a function comes from Python. Other languages use
883"function" which clashes with legacy Vim script.
884
885
886Type checking ~
887
888When compiling lines of Vim commands into instructions as much as possible
889should be done at compile time. Postponing it to runtime makes the execution
890slower and means mistakes are found only later. For example, when
891encountering the "+" character and compiling this into a generic add
892instruction, at execution time the instruction would have to inspect the type
893of the arguments and decide what kind of addition to do. And when the
894type is dictionary throw an error. If the types are known to be numbers then
895an "add number" instruction can be used, which is faster. The error can be
896given at compile time, no error handling is needed at runtime.
897
898The syntax for types is similar to Java, since it is easy to understand and
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200899widely used. The type names are what were used in Vim before, with some
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900additions such as "void" and "bool".
901
902
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200903Compiling functions early ~
904
905Functions are compiled when called or when `:defcompile` is used. Why not
906compile them early, so that syntax and type errors are reported early?
907
908The functions can't be compiled right away when encountered, because there may
909be forward references to functions defined later. Consider defining functions
910A, B and C, where A calls B, B calls C, and C calls A again. It's impossible
911to reorder the functions to avoid forward references.
912
913An alternative would be to first scan through the file to locate items and
Bram Moolenaar73fef332020-06-21 22:12:03 +0200914figure out their type, so that forward references are found, and only then
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200915execute the script and compile the functions. This means the script has to be
916parsed twice, which is slower, and some conditions at the script level, such
917as checking if a feature is supported, are hard to use. An attempt was made
918to see if it works, but it turned out to be impossible to make work nicely.
919
920It would be possible to compile all the functions at the end of the script.
921The drawback is that if a function never gets called, the overhead of
922compiling it counts anyway. Since startup speed is very important, in most
923cases it's better to do it later and accept that syntax and type errors are
924only reported then. In case these errors should be found early, e.g. when
925testing, the `:defcompile` command will help out.
926
927
928TypeScript syntax and semantics ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929
930Script writers have complained that the Vim script syntax is unexpectedly
931different from what they are used to. To reduce this complaint popular
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200932languages are used as an example. At the same time, we do not want to abandon
933the well-known parts of legacy Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935Since Vim already uses `:let` and `:const` and optional type checking is
936desirable, the JavaScript/TypeScript syntax fits best for variable
937declarations. >
938 const greeting = 'hello' " string type is inferred
939 let name: string
940 ...
941 name = 'John'
942
943Expression evaluation was already close to what JavaScript and other languages
944are doing. Some details are unexpected and can be fixed. For example how the
945|| and && operators work. Legacy Vim script: >
946 let result = 44
947 ...
948 return result || 0 " returns 1
949
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200950Vim9 script works like JavaScript/TypeScript, keep the value: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 let result = 44
952 ...
953 return result || 0 " returns 44
954
955On the other hand, overloading "+" to use both for addition and string
956concatenation goes against legacy Vim script and often leads to mistakes.
957For that reason we will keep using ".." for string concatenation. Lua also
958uses ".." this way.
959
960
961Import and Export ~
962
963A problem of legacy Vim script is that by default all functions and variables
964are global. It is possible to make them script-local, but then they are not
965available in other scripts.
966
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200967In Vim9 script a mechanism very similar to the JavaScript import and export
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968mechanism is supported. It is a variant to the existing `:source` command
969that works like one would expect:
970- Instead of making everything global by default, everything is script-local,
971 unless exported.
972- When importing a script the symbols that are imported are listed, avoiding
973 name conflicts and failures if later functionality is added.
974- The mechanism allows for writing a big, long script with a very clear API:
975 the exported function(s) and class(es).
976- By using relative paths loading can be much faster for an import inside of a
977 package, no need to search many directories.
978- Once an import has been used, it can be cached and loading it again can be
979 avoided.
980- The Vim-specific use of "s:" to make things script-local can be dropped.
981
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200982When sourcing a Vim9 script from a legacy script, only the items defined
983globally can be used, not the exported items. Alternatives considered:
984- All the exported items become available as script-local items. This makes
985 it uncontrollable what items get defined.
986- Use the exported items and make them global. Disadvantage is that it's then
987 not possible to avoid name clashes in the global namespace.
988- Completely disallow sourcing a Vim9 script, require using `:import`. That
989 makes it difficult to use scripts for testing, or sourcing them from the
990 command line to try them out.
991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992
993Classes ~
994
995Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But
996these have never become widespread. When Vim 9 was designed a decision was
997made to phase out these interfaces and concentrate on Vim script, while
998encouraging plugin authors to write code in any language and run it as an
999external tool, using jobs and channels.
1000
1001Still, using an external tool has disadvantages. An alternative is to convert
1002the tool into Vim script. For that to be possible without too much
1003translation, and keeping the code fast at the same time, the constructs of the
1004tool need to be supported. Since most languages support classes the lack of
1005class support in Vim is then a problem.
1006
1007Previously Vim supported a kind-of object oriented programming by adding
1008methods to a dictionary. With some care this could be made to work, but it
1009does not look like real classes. On top of that, it's very slow, because of
1010the use of dictionaries.
1011
1012The support of classes in Vim9 script is a "minimal common functionality" of
1013class support in most languages. It works mostly like Java, which is the most
1014popular programming language.
1015
1016
1017
1018 vim:tw=78:ts=8:noet:ft=help:norl: