blob: 576479b35b34e2af8f686c8d7ab544b47c47fe10 [file] [log] [blame]
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001*vim9.txt* For Vim version 8.2. Last change: 2020 Sep 17
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|
Bram Moolenaar1d59aa12020-09-19 18:50:13 +0200226. Future work: classes |vim9-classes|
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010023
249. Rationale |vim9-rationale|
25
26==============================================================================
27
281. What is Vim9 script? *vim9-script*
29
30THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
31
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020032Vim script has been growing over time, while preserving backwards
33compatibility. That means bad choices from the past often can't be changed
Bram Moolenaar73fef332020-06-21 22:12:03 +020034and compatibility with Vi restricts possible solutions. Execution is quite
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020035slow, each line is parsed every time it is executed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020037The main goal of Vim9 script is to drastically improve performance. This is
38accomplished by compiling commands into instructions that can be efficiently
39executed. An increase in execution speed of 10 to 100 times can be expected.
40
41A secondary goal is to avoid Vim-specific constructs and get closer to
42commonly used programming languages, such as JavaScript, TypeScript and Java.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010043
44The performance improvements can only be achieved by not being 100% backwards
Bram Moolenaar388a5d42020-05-26 21:20:45 +020045compatible. For example, making function arguments available in the
46"a:" dictionary adds quite a lot of overhead. In a Vim9 function this
47dictionary is not available. Other differences are more subtle, such as how
48errors are handled.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049
50The Vim9 script syntax and semantics are used in:
51- a function defined with the `:def` command
52- a script file where the first command is `vim9script`
Bram Moolenaar1d59aa12020-09-19 18:50:13 +020053- an autocommand defined in the context of the above
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010054
55When using `:function` in a Vim9 script file the legacy syntax is used.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020056However, this can be confusing and is therefore discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010057
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020058Vim9 script and legacy Vim script can be mixed. There is no requirement to
Bram Moolenaar1d59aa12020-09-19 18:50:13 +020059rewrite old scripts, they keep working as before. You may want to use a few
60`:def` functions for code that needs to be fast.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061
62==============================================================================
63
642. Differences from legacy Vim script *vim9-differences*
65
66THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
67
Bram Moolenaar2c330432020-04-13 14:41:35 +020068Comments starting with # ~
69
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020070In legacy Vim script comments start with double quote. In Vim9 script
71comments start with #. >
72 # declarations
Bram Moolenaar73fef332020-06-21 22:12:03 +020073 let count = 0 # number of occurrences
Bram Moolenaar2c330432020-04-13 14:41:35 +020074
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020075The reason is that a double quote can also be the start of a string. In many
Bram Moolenaar3d1cde82020-08-15 18:55:18 +020076places, especially halfway through an expression with a line break, it's hard
77to tell what the meaning is, since both a string and a comment can be followed
78by arbitrary text. To avoid confusion only # comments are recognized. This
79is the same as in shell scripts and Python programs.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020080
81In Vi # is a command to list text with numbers. In Vim9 script you can use
82`:number` for that. >
Bram Moolenaarae616492020-07-28 20:07:27 +020083 101 number
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020084
85To improve readability there must be a space between a command and the #
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020086that starts a comment. Note that #{ is the start of a dictionary, therefore
Bram Moolenaarae616492020-07-28 20:07:27 +020087it does not start a comment.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020088
Bram Moolenaar2c330432020-04-13 14:41:35 +020089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090Vim9 functions ~
91
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020092A function defined with `:def` is compiled. Execution is many times faster,
93often 10x to 100x times.
94
Bram Moolenaar388a5d42020-05-26 21:20:45 +020095Many errors are already found when compiling, before the function is executed.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020096The syntax is strict, to enforce code that is easy to read and understand.
97
Bram Moolenaar207f0092020-08-30 17:20:20 +020098Compilation is done when:
99- the function is first called
100- when the `:defcompile` command is encountered in the script where the
101 function was defined
102- `:disassemble` is used for the function.
103- a function that is compiled calls the function or uses it as a function
104 reference
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200105
106`:def` has no options like `:function` does: "range", "abort", "dict" or
107"closure". A `:def` function always aborts on an error, does not get a range
108passed and cannot be a "dict" function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200110The argument types and return type need to be specified. The "any" type can
111be used, type checking will then be done at runtime, like with legacy
112functions.
113
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200114Arguments are accessed by name, without "a:", just like any other language.
115There is no "a:" dictionary or "a:000" list.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200116
117Variable arguments are defined as the last argument, with a name and have a
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200118list type, similar to TypeScript. For example, a list of numbers: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200119 def MyFunc(...itemlist: list<number>)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 for item in itemlist
121 ...
122
123
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200124Functions and variables are script-local by default ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200125 *vim9-scopes*
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200126When using `:function` or `:def` to specify a new function at the script level
127in a Vim9 script, the function is local to the script, as if "s:" was
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200128prefixed. Using the "s:" prefix is optional. To define or use a global
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200129function or variable the "g:" prefix should be used. For functions in an
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200130autoload script the "name#" prefix is sufficient. >
131 def ThisFunction() # script-local
132 def s:ThisFunction() # script-local
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200133 def g:ThatFunction() # global
134 def ThatFunction() # global if no local ThatFunction()
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200135 def scriptname#function() # autoload
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200136
137When using `:function` or `:def` to specify a new function inside a function,
138the function is local to the function. It is not possible to define a
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200139script-local function inside a function. It is possible to define a global
140function, using the "g:" prefix.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200141
142When referring to a function and no "s:" or "g:" prefix is used, Vim will
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200143prefer using a local function (in the function scope, script scope or
144imported) before looking for a global function.
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200145In all cases the function must be defined before used. That is when it is
146first called or when `:defcompile` causes the call to be compiled.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200147
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200148The result is that functions and variables without a namespace can usually be
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200149found in the script, either defined there or imported. Global functions and
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200150variables could be defined anywhere (good luck finding out where!).
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200151
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200152Global functions can still be defined and deleted at nearly any time. In
Bram Moolenaar2cfb4a22020-05-07 18:56:00 +0200153Vim9 script script-local functions are defined once when the script is sourced
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200154and cannot be deleted or replaced.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200155
156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157Variable declarations with :let and :const ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200158 *vim9-declaration*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159Local variables need to be declared with `:let`. Local constants need to be
160declared with `:const`. We refer to both as "variables".
161
162Variables can be local to a script, function or code block: >
163 vim9script
164 let script_var = 123
165 def SomeFunc()
166 let func_var = script_var
167 if cond
168 let block_var = func_var
169 ...
170
171The variables are only visible in the block where they are defined and nested
172blocks. Once the block ends the variable is no longer accessible: >
173 if cond
174 let inner = 5
175 else
176 let inner = 0
177 endif
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200178 echo inner # Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179
180The declaration must be done earlier: >
181 let inner: number
182 if cond
183 inner = 5
184 else
185 inner = 0
186 endif
187 echo inner
188
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200189To intentionally avoid a variable being available later, a block can be used:
190>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100191 {
192 let temp = 'temp'
193 ...
194 }
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200195 echo temp # Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200197Declaring a variable with a type but without an initializer will initialize to
198zero, false or empty.
199
Bram Moolenaar560979e2020-02-04 22:53:05 +0100200An existing variable cannot be assigned to with `:let`, since that implies a
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200201declaration. Global, window, tab, buffer and Vim variables can only be used
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200202without `:let`, because they are not really declared, they can also be deleted
203with `:unlet`.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200205Variables and functions cannot shadow previously defined or imported variables
206and functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207Variables may shadow Ex commands, rename the variable if needed.
208
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200209Global variables and user defined functions must be prefixed with "g:", also
210at the script level. >
Bram Moolenaard1caa942020-04-10 22:10:56 +0200211 vim9script
212 let script_local = 'text'
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200213 g:global = 'value'
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200214 let Funcref = g:ThatFunction
Bram Moolenaard1caa942020-04-10 22:10:56 +0200215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be
217used to repeat a `:substitute` command.
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200218 *vim9-const*
219In legacy Vim script "const list = []" would make the variable "list"
220immutable and also the value. Thus you cannot add items to the list. This
221differs from what many languages do. Vim9 script does it like TypeScript: only
222"list" is immutable, the value can be changed.
223
224One can use `:const!` to make both the variable and the value immutable. Use
225this for composite structures that you want to make sure will not be modified.
226
227How this works: >
228 vim9script
229 const list = [1, 2]
230 list = [3, 4] # Error!
231 list[0] = 2 # OK
232
233 const! LIST = [1, 2]
234 LIST = [3, 4] # Error!
235 LIST[0] = 2 # Error!
236It is common to write constants as ALL_CAPS, but you don't have to.
237
238The constant only applies to the value itself, not what it refers to. >
239 cont females = ["Mary"]
240 const! NAMES = [["John", "Peter"], females]
241 NAMES[0] = ["Jack"] # Error!
242 NAMES[0][0] = ["Jack"] # Error!
243 NAMES[1] = ["Emma"] # Error!
244 Names[1][0] = "Emma" # OK, now females[0] == "Emma"
245
246Rationale: TypeScript has no way to make the value immutable. One can use
247immutable types, but that quickly gets complicated for nested values. And
248with a type cast the value can be made mutable again, which means there is no
249guarantee the value won't change. Vim supports immutable values, in legacy
250script this was done with `:lockvar`. But that is an extra statement and also
251applies to nested values. Therefore the solution to use `:const!`.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200253 *E1092*
254Declaring more than one variable at a time, using the unpack notation, is
255currently not supported: >
256 let [v1, v2] = GetValues() # Error!
257That is because the type needs to be inferred from the list item type, which
258isn't that easy.
259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260
261Omitting :call and :eval ~
262
263Functions can be called without `:call`: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200264 writefile(lines, 'file')
Bram Moolenaar560979e2020-02-04 22:53:05 +0100265Using `:call` is still possible, but this is discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266
267A method call without `eval` is possible, so long as the start is an
Bram Moolenaarae616492020-07-28 20:07:27 +0200268identifier or can't be an Ex command. Examples: >
269 myList->add(123)
270 g:myList->add(123)
271 [1, 2, 3]->Process()
272 #{a: 1, b: 2}->Process()
273 {'a': 1, 'b': 2}->Process()
274 "foobar"->Process()
275 ("foobar")->Process()
276 'foobar'->Process()
277 ('foobar')->Process()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200279In the rare case there is ambiguity between a function name and an Ex command,
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200280prepend ":" to make clear you want to use the Ex command. For example, there
281is both the `:substitute` command and the `substitute()` function. When the
282line starts with `substitute(` this will use the function. Prepend a colon to
283use the command instead: >
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100284 :substitute(pattern (replacement (
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100285
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100286Note that while variables need to be defined before they can be used,
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200287functions can be called before being defined. This is required to allow
288for cyclic dependencies between functions. It is slightly less efficient,
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100289since the function has to be looked up by name. And a typo in the function
Bram Moolenaarae616492020-07-28 20:07:27 +0200290name will only be found when the function is called.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292
Bram Moolenaard1caa942020-04-10 22:10:56 +0200293Omitting function() ~
294
295A user defined function can be used as a function reference in an expression
296without `function()`. The argument types and return type will then be checked.
297The function must already have been defined. >
298
299 let Funcref = MyFunction
300
301When using `function()` the resulting type is "func", a function with any
302number of arguments and any return type. The function can be defined later.
303
304
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200305Automatic line continuation ~
306
307In many cases it is obvious that an expression continues on the next line. In
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200308those cases there is no need to prefix the line with a backslash
309|line-continuation|. For example, when a list spans multiple lines: >
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200310 let mylist = [
311 'one',
312 'two',
313 ]
Bram Moolenaare6085c52020-04-12 20:19:16 +0200314And when a dict spans multiple lines: >
315 let mydict = #{
316 one: 1,
317 two: 2,
318 }
319Function call: >
320 let result = Func(
321 arg1,
322 arg2
323 )
324
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200325For binary operators in expressions not in [], {} or () a line break is
326possible just before or after the operator. For example: >
327 let text = lead
328 .. middle
329 .. end
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200330 let total = start +
331 end -
332 correction
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200333 let result = positive
334 ? PosFunc(arg)
335 : NegFunc(arg)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200336
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200337For a method call using "->" and a member using a dot, a line break is allowed
338before it: >
Bram Moolenaar73fef332020-06-21 22:12:03 +0200339 let result = GetBuilder()
340 ->BuilderSetWidth(333)
341 ->BuilderSetHeight(777)
342 ->BuilderBuild()
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200343 let result = MyDict
344 .member
Bram Moolenaar73fef332020-06-21 22:12:03 +0200345
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200346< *E1050*
347To make it possible for the operator at the start of the line to be
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200348recognized, it is required to put a colon before a range. This will add
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200349"start" and print: >
350 let result = start
351 + print
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200352Like this: >
353 let result = start + print
354
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200355This will assign "start" and print a line: >
356 let result = start
357 :+ print
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200358
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200359It is also possible to split a function header over multiple lines, in between
360arguments: >
361 def MyFunc(
362 text: string,
363 separator = '-'
364 ): string
365
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200366Notes:
367- "enddef" cannot be used at the start of a continuation line, it ends the
368 current function.
369- No line break is allowed in the LHS of an assignment. Specifically when
370 unpacking a list |:let-unpack|. This is OK: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200371 [var1, var2] =
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200372 Func()
373< This does not work: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200374 [var1,
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200375 var2] =
376 Func()
377- No line break is allowed in between arguments of an `:echo`, `:execute` and
378 similar commands. This is OK: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200379 echo [1,
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200380 2] [3,
381 4]
382< This does not work: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200383 echo [1, 2]
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200384 [3, 4]
385- No line break is allowed in the arguments of a lambda, between the "{" and
386 "->". This is OK: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200387 filter(list, {k, v ->
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200388 v > 0})
389< This does not work: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200390 filter(list, {k,
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200391 v -> v > 0})
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200392
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100394No curly braces expansion ~
395
396|curly-braces-names| cannot be used.
397
398
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200399No :xit, :t, :append, :change or :insert ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200401These commands are too easily confused with local variable names.
402Instead of `:x` or `:xit` you can use `:exit`.
403Instead of `:t` you can use `:copy`.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100404
405
406Comparators ~
407
408The 'ignorecase' option is not used for comparators that use strings.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409
410
411White space ~
412
413Vim9 script enforces proper use of white space. This is no longer allowed: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200414 let var=234 # Error!
415 let var= 234 # Error!
416 let var =234 # Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100417There must be white space before and after the "=": >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200418 let var = 234 # OK
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200419White space must also be put before the # that starts a comment after a
420command: >
Bram Moolenaar2c330432020-04-13 14:41:35 +0200421 let var = 234# Error!
422 let var = 234 # OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423
424White space is required around most operators.
425
426White space is not allowed:
427- Between a function name and the "(": >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200428 call Func (arg) # Error!
429 call Func
430 \ (arg) # Error!
431 call Func(arg) # OK
432 call Func(
433 \ arg) # OK
434 call Func(
435 \ arg # OK
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100436 \ )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437
438
439Conditions and expressions ~
440
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200441Conditions and expressions are mostly working like they do in JavaScript. A
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100442difference is made where JavaScript does not work like most people expect.
443Specifically, an empty list is falsey.
444
445Any type of variable can be used as a condition, there is no error, not even
446for using a list or job. This is very much like JavaScript, but there are a
447few exceptions.
448
449 type TRUE when ~
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200450 bool v:true or 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100451 number non-zero
452 float non-zero
453 string non-empty
454 blob non-empty
455 list non-empty (different from JavaScript)
456 dictionary non-empty (different from JavaScript)
Bram Moolenaard1caa942020-04-10 22:10:56 +0200457 func when there is a function name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 special v:true
459 job when not NULL
460 channel when not NULL
461 class when not NULL
462 object when not NULL (TODO: when isTrue() returns v:true)
463
464The boolean operators "||" and "&&" do not change the value: >
465 8 || 2 == 8
466 0 || 2 == 2
467 0 || '' == ''
468 8 && 2 == 2
469 0 && 2 == 0
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200470 2 && 0 == 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471 [] && 2 == []
472
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200473When using `..` for string concatenation arguments of simple types are always
474converted to string. >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 'hello ' .. 123 == 'hello 123'
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200476 'hello ' .. v:true == 'hello v:true'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200478Simple types are string, float, special and bool. For other types |string()|
479can be used.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200480 *false* *true*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481In Vim9 script one can use "true" for v:true and "false" for v:false.
482
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200483Indexing a string with [idx] or [idx, idx] uses character indexes instead of
484byte indexes. Example: >
485 echo 'bár'[1]
486In legacy script this results in the character 0xc3 (an illegal byte), in Vim9
487script this results in the string 'á'.
488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489
Bram Moolenaare46a4402020-06-30 20:38:27 +0200490What to watch out for ~
491 *vim9-gotchas*
492Vim9 was designed to be closer to often used programming languages, but at the
493same time tries to support the legacy Vim commands. Some compromises had to
494be made. Here is a summary of what might be unexpected.
495
496Ex command ranges need to be prefixed with a colon. >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200497 -> # legacy Vim: shifts the previous line to the right
498 ->func() # Vim9: method call in continuation line
499 :-> # Vim9: shifts the previous line to the right
Bram Moolenaare46a4402020-06-30 20:38:27 +0200500
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200501 %s/a/b # legacy Vim: substitute on all lines
Bram Moolenaare46a4402020-06-30 20:38:27 +0200502 x = alongname
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200503 % another # Vim9: line continuation without a backslash
504 :%s/a/b # Vim9: substitute on all lines
505 'text'->func() # Vim9: method call
506 :'t # legacy Vim: jump to mark m
Bram Moolenaare46a4402020-06-30 20:38:27 +0200507
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200508Some Ex commands can be confused with assignments in Vim9 script: >
509 g:name = value # assignment
510 g:pattern:cmd # invalid command - ERROR
511 :g:pattern:cmd # :global command
512
Bram Moolenaare46a4402020-06-30 20:38:27 +0200513Functions defined with `:def` compile the whole function. Legacy functions
514can bail out, and the following lines are not parsed: >
515 func Maybe()
516 if !has('feature')
517 return
518 endif
519 use-feature
520 endfunc
521Vim9 functions are compiled as a whole: >
522 def Maybe()
523 if !has('feature')
524 return
525 endif
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200526 use-feature # May give compilation error
Bram Moolenaare46a4402020-06-30 20:38:27 +0200527 enddef
528For a workaround, split it in two functions: >
529 func Maybe()
530 if has('feature')
531 call MaybyInner()
532 endif
533 endfunc
534 if has('feature')
535 def MaybeInner()
536 use-feature
537 enddef
538 endif
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200539Or put the unsupported code inside an `if` with a constant expression that
Bram Moolenaar207f0092020-08-30 17:20:20 +0200540evaluates to false: >
541 def Maybe()
542 if has('feature')
543 use-feature
544 endif
545 enddef
546Note that for unrecognized commands there is no check for "|" and a following
547command. This will give an error for missing `endif`: >
548 def Maybe()
549 if has('feature') | use-feature | endif
550 enddef
Bram Moolenaare46a4402020-06-30 20:38:27 +0200551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552==============================================================================
553
5543. New style functions *fast-functions*
555
556THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
557
558 *:def*
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200559:def[!] {name}([arguments])[: {return-type}]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 Define a new function by the name {name}. The body of
561 the function follows in the next lines, until the
562 matching `:enddef`.
563
Bram Moolenaard77a8522020-04-03 21:59:57 +0200564 When {return-type} is omitted or is "void" the
565 function is not expected to return anything.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566
567 {arguments} is a sequence of zero or more argument
568 declarations. There are three forms:
569 {name}: {type}
570 {name} = {value}
571 {name}: {type} = {value}
572 The first form is a mandatory argument, the caller
573 must always provide them.
574 The second and third form are optional arguments.
575 When the caller omits an argument the {value} is used.
576
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200577 The function will be compiled into instructions when
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200578 called, or when `:disassemble` or `:defcompile` is
579 used. Syntax and type errors will be produced at that
580 time.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200581
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200582 It is possible to nest `:def` inside another `:def` or
583 `:function` up to about 50 levels deep.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100584
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200585 [!] is used as with `:function`. Note that in Vim9
586 script script-local functions cannot be deleted or
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200587 redefined later in the same script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588
589 *:enddef*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200590:enddef End of a function defined with `:def`. It should be on
591 a line by its own.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592
593
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100594If the script the function is defined in is Vim9 script, then script-local
595variables can be accessed without the "s:" prefix. They must be defined
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200596before the function is compiled. If the script the function is defined in is
597legacy script, then script-local variables must be accessed with the "s:"
Bram Moolenaar207f0092020-08-30 17:20:20 +0200598prefix and they do not need to exist (they can be deleted any time).
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100599
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200600 *:defc* *:defcompile*
601:defc[ompile] Compile functions defined in the current script that
602 were not compiled yet.
603 This will report errors found during the compilation.
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100604
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100605 *:disa* *:disassemble*
606:disa[ssemble] {func} Show the instructions generated for {func}.
607 This is for debugging and testing.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100608 Note that for command line completion of {func} you
609 can prepend "s:" to find script-local functions.
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100610
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200611Limitations ~
612
613Local variables will not be visible to string evaluation. For example: >
614 def EvalString(): list<string>
615 let list = ['aa', 'bb', 'cc', 'dd']
616 return range(1, 2)->map('list[v:val]')
617 enddef
618
619The map argument is a string expression, which is evaluated without the
620function scope. Instead, use a lambda: >
621 def EvalString(): list<string>
622 let list = ['aa', 'bb', 'cc', 'dd']
623 return range(1, 2)->map({ _, v -> list[v] })
624 enddef
625
626
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627==============================================================================
628
6294. Types *vim9-types*
630
631THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
632
633The following builtin types are supported:
634 bool
635 number
636 float
637 string
638 blob
Bram Moolenaard77a8522020-04-03 21:59:57 +0200639 list<{type}>
640 dict<{type}>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 job
642 channel
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100643 func
Bram Moolenaard1caa942020-04-10 22:10:56 +0200644 func: {type}
Bram Moolenaard77a8522020-04-03 21:59:57 +0200645 func({type}, ...)
646 func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647
648Not supported yet:
Bram Moolenaard77a8522020-04-03 21:59:57 +0200649 tuple<a: {type}, b: {type}, ...>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650
Bram Moolenaard77a8522020-04-03 21:59:57 +0200651These types can be used in declarations, but no value will have this type:
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200652 {type}|{type} {not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 void
654 any
655
Bram Moolenaard77a8522020-04-03 21:59:57 +0200656There is no array type, use list<{type}> instead. For a list constant an
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657efficient implementation is used that avoids allocating lot of small pieces of
658memory.
659
Bram Moolenaard77a8522020-04-03 21:59:57 +0200660A partial and function can be declared in more or less specific ways:
661func any kind of function reference, no type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200662 checking for arguments or return value
Bram Moolenaard77a8522020-04-03 21:59:57 +0200663func: {type} any number and type of arguments with specific
664 return type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200665func({type}) function with argument type, does not return
Bram Moolenaard77a8522020-04-03 21:59:57 +0200666 a value
Bram Moolenaard1caa942020-04-10 22:10:56 +0200667func({type}): {type} function with argument type and return type
668func(?{type}) function with type of optional argument, does
669 not return a value
670func(...{type}) function with type of variable number of
671 arguments, does not return a value
672func({type}, ?{type}, ...{type}): {type}
673 function with:
674 - type of mandatory argument
675 - type of optional argument
676 - type of variable number of arguments
677 - return type
Bram Moolenaard77a8522020-04-03 21:59:57 +0200678
679If the return type is "void" the function does not return a value.
680
681The reference can also be a |Partial|, in which case it stores extra arguments
682and/or a dictionary, which are not visible to the caller. Since they are
683called in the same way the declaration is the same.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684
685Custom types can be defined with `:type`: >
686 :type MyList list<string>
Bram Moolenaar127542b2020-08-09 17:22:04 +0200687Custom types must start with a capital letter, to avoid name clashes with
688builtin types added later, similarly to user functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689{not implemented yet}
690
691And classes and interfaces can be used as types: >
692 :class MyClass
693 :let mine: MyClass
694
695 :interface MyInterface
696 :let mine: MyInterface
697
698 :class MyTemplate<Targ>
699 :let mine: MyTemplate<number>
700 :let mine: MyTemplate<string>
701
702 :class MyInterface<Targ>
703 :let mine: MyInterface<number>
704 :let mine: MyInterface<string>
705{not implemented yet}
706
707
Bram Moolenaar64d662d2020-08-09 19:02:50 +0200708Variable types and type casting *variable-types*
709
710Variables declared in Vim9 script or in a `:def` function have a type, either
711specified explicitly or inferred from the initialization.
712
713Global, buffer, window and tab page variables do not have a specific type, the
714value can be changed at any time, possibly changing the type. Therefore, in
715compiled code the "any" type is assumed.
716
717This can be a problem when the "any" type is undesired and the actual type is
718expected to always be the same. For example, when declaring a list: >
719 let l: list<number> = [1, g:two]
720This will give an error, because "g:two" has type "any". To avoid this, use a
721type cast: >
722 let l: list<number> = [1, <number>g:two]
723< *type-casting*
724The compiled code will then check that "g:two" is a number at runtime and give
725an error if it isn't. This is called type casting.
726
727The syntax of a type cast is: "<" {type} ">". There cannot be white space
728after the "<" or before the ">" (to avoid them being confused with
729smaller-than and bigger-than operators).
730
731The semantics is that, if needed, a runtime type check is performed. The
732value is not actually changed. If you need to change the type, e.g. to change
733it to a string, use the |string()| function. Or use |str2nr()| to convert a
734string to a number.
735
736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737Type inference *type-inference*
738
739In general: Whenever the type is clear it can be omitted. For example, when
740declaring a variable and giving it a value: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200741 let var = 0 # infers number type
742 let var = 'hello' # infers string type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
Bram Moolenaar127542b2020-08-09 17:22:04 +0200744The type of a list and dictionary comes from the common type of the values.
745If the values all have the same type, that type is used for the list or
746dictionary. If there is a mix of types, the "any" type is used. >
747 [1, 2, 3] list<number>
748 ['a', 'b', 'c'] list<string>
749 [1, 'x', 3] list<any>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750
Bram Moolenaar207f0092020-08-30 17:20:20 +0200751
752Stricter type checking *type-checking*
753
754In legacy Vim script, where a number was expected, a string would be
755automatically converted to a number. This was convenient for an actual number
756such as "123", but leads to unexpected problems (but no error message) if the
757string doesn't start with a number. Quite often this leads to hard-to-find
758bugs.
759
760In Vim9 script this has been made stricter. In most places it works just as
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200761before, if the value used matches the expected type. There will sometimes be
762an error, thus breaking backwards compatibility. For example:
Bram Moolenaar207f0092020-08-30 17:20:20 +0200763- Using a number other than 0 or 1 where a boolean is expected. *E1023*
764- Using a string value when setting a number options.
765- Using a number where a string is expected. *E1024*
766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767==============================================================================
768
7695. Namespace, Import and Export
770 *vim9script* *vim9-export* *vim9-import*
771
772THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
773
774A Vim9 script can be written to be imported. This means that everything in
775the script is local, unless exported. Those exported items, and only those
776items, can then be imported in another script.
777
Bram Moolenaar207f0092020-08-30 17:20:20 +0200778You can cheat by using the global namespace explicitly. We will assume here
779that you don't do that.
780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781
782Namespace ~
783 *:vim9script* *:vim9*
Bram Moolenaar560979e2020-02-04 22:53:05 +0100784To recognize a file that can be imported the `vim9script` statement must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785appear as the first statement in the file. It tells Vim to interpret the
786script in its own namespace, instead of the global namespace. If a file
787starts with: >
788 vim9script
789 let myvar = 'yes'
790Then "myvar" will only exist in this file. While without `vim9script` it would
791be available as `g:myvar` from any other script and function.
792
793The variables at the file level are very much like the script-local "s:"
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200794variables in legacy Vim script, but the "s:" is omitted. And they cannot be
795deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200797In Vim9 script the global "g:" namespace can still be used as before. And the
798"w:", "b:" and "t:" namespaces. These have in common that variables are not
799declared and they can be deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800
801A side effect of `:vim9script` is that the 'cpoptions' option is set to the
802Vim default value, like with: >
803 :set cpo&vim
804One of the effects is that |line-continuation| is always enabled.
805The original value of 'cpoptions' is restored at the end of the script.
806
807
808Export ~
809 *:export* *:exp*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200810Exporting an item can be written as: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 export const EXPORTED_CONST = 1234
812 export let someValue = ...
813 export def MyFunc() ...
814 export class MyClass ...
815
816As this suggests, only constants, variables, `:def` functions and classes can
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200817be exported. {classes are not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200819 *E1042*
820`:export` can only be used in Vim9 script, at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821
822
823Import ~
Bram Moolenaar73fef332020-06-21 22:12:03 +0200824 *:import* *:imp* *E1094*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825The exported items can be imported individually in another Vim9 script: >
826 import EXPORTED_CONST from "thatscript.vim"
827 import MyClass from "myclass.vim"
828
829To import multiple items at the same time: >
830 import {someValue, MyClass} from "thatscript.vim"
831
Bram Moolenaar560979e2020-02-04 22:53:05 +0100832In case the name is ambiguous, another name can be specified: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 import MyClass as ThatClass from "myclass.vim"
834 import {someValue, MyClass as ThatClass} from "myclass.vim"
835
836To import all exported items under a specific identifier: >
837 import * as That from 'thatscript.vim'
838
Bram Moolenaar1d59aa12020-09-19 18:50:13 +0200839{not implemented yet: using "This as That"}
840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free
842to choose the name "That", but it is highly recommended to use the name of the
843script file to avoid confusion.
844
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200845`:import` can also be used in legacy Vim script. The imported items still
846become script-local, even when the "s:" prefix is not given.
847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848The script name after `import` can be:
849- A relative path, starting "." or "..". This finds a file relative to the
850 location of the script file itself. This is useful to split up a large
851 plugin into several files.
852- An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This
853 will be rarely used.
854- A path not being relative or absolute. This will be found in the
855 "import" subdirectories of 'runtimepath' entries. The name will usually be
856 longer and unique, to avoid loading the wrong file.
857
858Once a vim9 script file has been imported, the result is cached and used the
859next time the same script is imported. It will not be read again.
860 *:import-cycle*
861The `import` commands are executed when encountered. If that script (directly
862or indirectly) imports the current script, then items defined after the
863`import` won't be processed yet. Therefore cyclic imports can exist, but may
864result in undefined items.
865
866
867Import in an autoload script ~
868
869For optimal startup speed, loading scripts should be postponed until they are
Bram Moolenaar560979e2020-02-04 22:53:05 +0100870actually needed. A recommended mechanism:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871
8721. In the plugin define user commands, functions and/or mappings that refer to
873 an autoload script. >
874 command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>)
875
876< This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen.
877
Bram Moolenaar3d1cde82020-08-15 18:55:18 +02008782. In the autoload script do the actual work. You can import items from
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 other files to split up functionality in appropriate pieces. >
880 vim9script
881 import FilterFunc from "../import/someother.vim"
882 def searchfor#Stuff(arg: string)
883 let filtered = FilterFunc(arg)
884 ...
885< This goes in .../autoload/searchfor.vim. "searchfor" in the file name
886 must be exactly the same as the prefix for the function name, that is how
887 Vim finds the file.
888
8893. Other functionality, possibly shared between plugins, contains the exported
890 items and any private items. >
891 vim9script
892 let localVar = 'local'
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200893 export def FilterFunc(arg: string): string
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 ...
895< This goes in .../import/someother.vim.
896
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200897When compiling a `:def` function and a function in an autoload script is
898encountered, the script is not loaded until the `:def` function is called.
899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
901Import in legacy Vim script ~
902
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200903If an `import` statement is used in legacy Vim script, the script-local "s:"
904namespace will be used for the imported item, even when "s:" is not specified.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905
906
907==============================================================================
908
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02009096. Future work: classes *vim9-classes*
910
911Above "class" was mentioned a few times, but it has not been implemented yet.
912Most of Vim9 script can be created without this funcionality, and since
913implementing classes is going to be a lot of work, it is left for the future.
914For now we'll just make sure classes can be added later.
915
916Thoughts:
917- `class` / `endclass`, everything in one file
918- Class names are always CamelCase
919- Single constructor
920- Single inheritance with `class ThisClass extends BaseClass`
921- `abstract class`
922- `interface` (Abstract class without any implementation)
923- `class SomeClass implements SomeInterface`
924- Generics for class: `class <Tkey, Tentry>`
925- Generics for function: `def <Tkey> GetLast(key: Tkey)`
926
927Again, much of this is from TypeScript.
928
929Some things that look like good additions:
930- Use a class as an interface (like Dart)
931- Extend a class with methods, using an import (like Dart)
932
933An important class that will be provided is "Promise". Since Vim is single
934threaded, connecting asynchronous operations is a natural way of allowing
935plugins to do their work without blocking the user. It's a uniform way to
936invoke callbacks and handle timeouts and errors.
937
938==============================================================================
939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009409. Rationale *vim9-rationale*
941
942The :def command ~
943
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200944Plugin writers have asked for a much faster Vim script. Investigations have
Bram Moolenaar560979e2020-02-04 22:53:05 +0100945shown that keeping the existing semantics of function calls make this close to
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946impossible, because of the overhead involved with calling a function, setting
947up the local function scope and executing lines. There are many details that
948need to be handled, such as error messages and exceptions. The need to create
949a dictionary for a: and l: scopes, the a:000 list and several others add too
950much overhead that cannot be avoided.
951
952Therefore the `:def` method to define a new-style function had to be added,
953which allows for a function with different semantics. Most things still work
954as before, but some parts do not. A new way to define a function was
955considered the best way to separate the old-style code from Vim9 script code.
956
957Using "def" to define a function comes from Python. Other languages use
958"function" which clashes with legacy Vim script.
959
960
961Type checking ~
962
963When compiling lines of Vim commands into instructions as much as possible
964should be done at compile time. Postponing it to runtime makes the execution
965slower and means mistakes are found only later. For example, when
966encountering the "+" character and compiling this into a generic add
967instruction, at execution time the instruction would have to inspect the type
968of the arguments and decide what kind of addition to do. And when the
969type is dictionary throw an error. If the types are known to be numbers then
970an "add number" instruction can be used, which is faster. The error can be
Bram Moolenaar1d59aa12020-09-19 18:50:13 +0200971given at compile time, no error handling is needed at runtime, adding two
972numbers cannot fail.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973
974The syntax for types is similar to Java, since it is easy to understand and
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200975widely used. The type names are what were used in Vim before, with some
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976additions such as "void" and "bool".
977
978
Bram Moolenaar1d59aa12020-09-19 18:50:13 +0200979Removing clutter and weirdness ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200980
Bram Moolenaar1d59aa12020-09-19 18:50:13 +0200981Once decided that `:def` functions have different syntax than legacy functions,
982we are free to add improvements to make the code more familiar for users who
983know popular programming languages. In other words: remove weird things that
984only Vim uses.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200985
Bram Moolenaar1d59aa12020-09-19 18:50:13 +0200986We can also remove clutter, mainly things that were done to make Vim script
987backwards compatible with good old Vi commands.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200988
Bram Moolenaar1d59aa12020-09-19 18:50:13 +0200989Examples:
990- Drop `:call` for calling a function and `:eval` for manipulating data.
991- Drop using a leading backslash for line continuation, automatically figure
992 out where an expression ends.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200993
Bram Moolenaar1d59aa12020-09-19 18:50:13 +0200994However, this does require that some things need to change:
995- Comments start with # instead of ", to avoid confusing them with strings.
996- Ex command ranges need to be prefixed with a colon, to avoid confusion with
997 expressions (single quote can be a string or a mark, "/" can be divide or a
998 search command, etc.).
999
1000Goal is to limit the differences. A good criteria is that when the old syntax
1001is used you are very likely to get an error message.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001002
1003
1004TypeScript syntax and semantics ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005
1006Script writers have complained that the Vim script syntax is unexpectedly
1007different from what they are used to. To reduce this complaint popular
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001008languages are used as an example. At the same time, we do not want to abandon
1009the well-known parts of legacy Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010
1011Since Vim already uses `:let` and `:const` and optional type checking is
1012desirable, the JavaScript/TypeScript syntax fits best for variable
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001013declarations: >
1014 const greeting = 'hello' # string type is inferred
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 let name: string
1016 ...
1017 name = 'John'
1018
1019Expression evaluation was already close to what JavaScript and other languages
1020are doing. Some details are unexpected and can be fixed. For example how the
1021|| and && operators work. Legacy Vim script: >
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001022 let value = 44
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 ...
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001024 let result = value || 0 # result == 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025
Bram Moolenaar3d1cde82020-08-15 18:55:18 +02001026Vim9 script works like JavaScript/TypeScript, keep the value: >
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001027 let value = 44
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 ...
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001029 let result = value || 0 # result == 44
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001031Another reason why TypeScript can be used as an example for Vim9 script is the
1032mix of static typing (a variable always has a known value type) and dynamic
1033typing (a variable can have different types, this hanges at runtime). Since
1034legacy Vim script is dynamically typed and a lot of existing functionality
1035(esp. builtin functions) depends on that, while static typing allows for much
1036faster execution, we need to have this mix in Vim9 script.
1037
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001038There is no intention to completely match TypeScript syntax and semantics. We
1039just want to take those parts that we can use for Vim and we expect Vim users
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001040will be happy with. TypeScript is a complex language with its own history,
1041advantages and disadvantages. To get an idea of the disadvantages read the
1042book: "JavaScript: The Good Parts". Or find the article "TypeScript: the good
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001043parts" and read the "Things to avoid" section.
1044
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001045People familiar with other languages (Java, Python, etc.) will also find
1046things in TypeScript that they do not like or do not understand. We'll try to
1047avoid those things.
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001048
1049Specific items from TypeScript we avoid:
1050- Overloading "+", using it both for addition and string concatenation. This
1051 goes against legacy Vim script and often leads to mistakes. For that reason
1052 we will keep using ".." for string concatenation. Lua also uses ".." this
1053 way. And it allows for conversion to string for more values.
1054- TypeScript can use an expression like "99 || 'yes'" in a condition, but
1055 cannot assign the value to a boolean. That is inconsistent and can be
1056 annoying. Vim recognizes an expression with && or || and allows using the
1057 result as a bool.
1058- TypeScript considers an empty string as Falsy, but an empty list or dict as
1059 Truthy. That is inconsistent. In Vim an empty list and dict are also
1060 Falsy.
1061- TypeScript has various "Readonly" types, which have limited usefulness,
1062 since a type cast can remove the immutable nature. Vim locks the value,
1063 which is more flexible, but is only checked at runtime.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065
1066Import and Export ~
1067
1068A problem of legacy Vim script is that by default all functions and variables
1069are global. It is possible to make them script-local, but then they are not
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001070available in other scripts. This defies the concept of a package that only
1071exports selected items and keeps the rest local.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072
Bram Moolenaar3d1cde82020-08-15 18:55:18 +02001073In Vim9 script a mechanism very similar to the JavaScript import and export
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074mechanism is supported. It is a variant to the existing `:source` command
1075that works like one would expect:
1076- Instead of making everything global by default, everything is script-local,
1077 unless exported.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001078- When importing a script the symbols that are imported are explicitly listed,
1079 avoiding name conflicts and failures if functionality is added later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080- The mechanism allows for writing a big, long script with a very clear API:
1081 the exported function(s) and class(es).
1082- By using relative paths loading can be much faster for an import inside of a
1083 package, no need to search many directories.
1084- Once an import has been used, it can be cached and loading it again can be
1085 avoided.
1086- The Vim-specific use of "s:" to make things script-local can be dropped.
1087
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001088When sourcing a Vim9 script from a legacy script, only the items defined
1089globally can be used, not the exported items. Alternatives considered:
1090- All the exported items become available as script-local items. This makes
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001091 it uncontrollable what items get defined and likely soon leads to trouble.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001092- Use the exported items and make them global. Disadvantage is that it's then
1093 not possible to avoid name clashes in the global namespace.
1094- Completely disallow sourcing a Vim9 script, require using `:import`. That
1095 makes it difficult to use scripts for testing, or sourcing them from the
1096 command line to try them out.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001097Note that you can also use `:import` in legacy Vim script, see above.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001100Compiling functions early ~
1101
1102Functions are compiled when called or when `:defcompile` is used. Why not
1103compile them early, so that syntax and type errors are reported early?
1104
1105The functions can't be compiled right away when encountered, because there may
1106be forward references to functions defined later. Consider defining functions
1107A, B and C, where A calls B, B calls C, and C calls A again. It's impossible
1108to reorder the functions to avoid forward references.
1109
1110An alternative would be to first scan through the file to locate items and
1111figure out their type, so that forward references are found, and only then
1112execute the script and compile the functions. This means the script has to be
1113parsed twice, which is slower, and some conditions at the script level, such
1114as checking if a feature is supported, are hard to use. An attempt was made
1115to see if it works, but it turned out to be impossible to make work nicely.
1116
1117It would be possible to compile all the functions at the end of the script.
1118The drawback is that if a function never gets called, the overhead of
1119compiling it counts anyway. Since startup speed is very important, in most
1120cases it's better to do it later and accept that syntax and type errors are
1121only reported then. In case these errors should be found early, e.g. when
1122testing, the `:defcompile` command will help out.
1123
1124
1125Why not use an embeded language? ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126
1127Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001128these interfaces have never become widely used, for various reasons. When
1129Vim9 was designed a decision was made to make these interfaces lower priority
1130and concentrate on Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001132Still, plugin writers may find other languages more familiar, want to use
1133existing libraries or see a performance benefit. We encourage plugin authors
1134to write code in any language and run it as an external tool, using jobs and
1135channels. We can try to make this easier somehow.
1136
1137Using an external tool also has disadvantages. An alternative is to convert
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138the tool into Vim script. For that to be possible without too much
1139translation, and keeping the code fast at the same time, the constructs of the
1140tool need to be supported. Since most languages support classes the lack of
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001141support for classes in Vim is then a problem.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001143
1144Classes ~
1145
1146Vim supports a kind-of object oriented programming by adding methods to a
1147dictionary. With some care this can be made to work, but it does not look
1148like real classes. On top of that, it's quite slow, because of the use of
1149dictionaries.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150
1151The support of classes in Vim9 script is a "minimal common functionality" of
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001152class support in most languages. It works much like Java, which is the most
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153popular programming language.
1154
1155
1156
1157 vim:tw=78:ts=8:noet:ft=help:norl: