blob: 7246ff87a6966d32d032eaebafecb221ba48eb12 [file] [log] [blame]
Bram Moolenaare0e39172021-01-25 21:14:57 +01001*vim9.txt* For Vim version 8.2. Last change: 2021 Jan 23
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 Moolenaardcc58e02020-12-28 20:53:21 +01009Vim9 script commands and expressions. *Vim9* *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 Moolenaar7e6a5152021-01-02 16:39:53 +0100171. 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
Bram Moolenaar2b327002020-12-26 15:39:31 +0100281. What is Vim9 script? *Vim9-script*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010029
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
Bram Moolenaar82be4842021-01-11 19:40:15 +010055When using `:function` in a Vim9 script file the legacy syntax is used, with
56the highest |scriptversion|. However, this can be confusing and is therefore
57discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020059Vim9 script and legacy Vim script can be mixed. There is no requirement to
Bram Moolenaar1d59aa12020-09-19 18:50:13 +020060rewrite old scripts, they keep working as before. You may want to use a few
61`:def` functions for code that needs to be fast.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010062
63==============================================================================
64
652. Differences from legacy Vim script *vim9-differences*
66
67THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
68
Bram Moolenaard58a3bf2020-09-28 21:48:16 +020069Overview ~
70
71Brief summary of the differences you will most often encounter when using Vim9
72script and `:def` functions; details are below:
73- Comments start with #, not ": >
Bram Moolenaar82be4842021-01-11 19:40:15 +010074 echo "hello" # comment
Bram Moolenaard58a3bf2020-09-28 21:48:16 +020075- Using a backslash for line continuation is hardly ever needed: >
Bram Moolenaar82be4842021-01-11 19:40:15 +010076 echo "hello "
Bram Moolenaard58a3bf2020-09-28 21:48:16 +020077 .. yourName
78 .. ", how are you?"
79- White space is required in many places.
80- Assign values without `:let`, declare variables with `:var`: >
Bram Moolenaar82be4842021-01-11 19:40:15 +010081 var count = 0
Bram Moolenaard58a3bf2020-09-28 21:48:16 +020082 count += 3
83- Constants can be declared with `:final` and `:const`: >
Bram Moolenaar82be4842021-01-11 19:40:15 +010084 final matches = [] # add matches
Bram Moolenaard58a3bf2020-09-28 21:48:16 +020085 const names = ['Betty', 'Peter'] # cannot be changed
86- `:final` cannot be used as an abbreviation of `:finally`.
87- Variables and functions are script-local by default.
88- Functions are declared with argument types and return type: >
89 def CallMe(count: number, message: string): bool
90- Call functions without `:call`: >
Bram Moolenaar82be4842021-01-11 19:40:15 +010091 writefile(['done'], 'file.txt')
Bram Moolenaard58a3bf2020-09-28 21:48:16 +020092- You cannot use `:xit`, `:t`, `:append`, `:change`, `:insert` or curly-braces
93 names.
94- A range before a command must be prefixed with a colon: >
Bram Moolenaar82be4842021-01-11 19:40:15 +010095 :%s/this/that
96- Unless mentioned specifically, the highest |scriptversion| is used.
Bram Moolenaard58a3bf2020-09-28 21:48:16 +020097
98
Bram Moolenaar2c330432020-04-13 14:41:35 +020099Comments starting with # ~
100
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200101In legacy Vim script comments start with double quote. In Vim9 script
102comments start with #. >
103 # declarations
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200104 var count = 0 # number of occurrences
Bram Moolenaar2c330432020-04-13 14:41:35 +0200105
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200106The reason is that a double quote can also be the start of a string. In many
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200107places, especially halfway through an expression with a line break, it's hard
108to tell what the meaning is, since both a string and a comment can be followed
109by arbitrary text. To avoid confusion only # comments are recognized. This
110is the same as in shell scripts and Python programs.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200111
112In Vi # is a command to list text with numbers. In Vim9 script you can use
113`:number` for that. >
Bram Moolenaarae616492020-07-28 20:07:27 +0200114 101 number
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200115
116To improve readability there must be a space between a command and the #
Bram Moolenaar2b327002020-12-26 15:39:31 +0100117that starts a comment: >
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100118 var name = value # comment
119 var name = value# error!
Bram Moolenaar2b327002020-12-26 15:39:31 +0100120
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100121In legacy Vim script # is also used for the alternate file name. In Vim9
122script you need to use %% instead. Instead of ## use %%% (stands for all
123arguments).
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200124
Bram Moolenaar2c330432020-04-13 14:41:35 +0200125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100126Vim9 functions ~
127
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200128A function defined with `:def` is compiled. Execution is many times faster,
129often 10x to 100x times.
130
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200131Many errors are already found when compiling, before the function is executed.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200132The syntax is strict, to enforce code that is easy to read and understand.
133
Bram Moolenaar1b884a02020-12-10 21:11:27 +0100134Compilation is done when either of these is encountered:
135- the first time the function is called
Bram Moolenaar207f0092020-08-30 17:20:20 +0200136- when the `:defcompile` command is encountered in the script where the
137 function was defined
138- `:disassemble` is used for the function.
139- a function that is compiled calls the function or uses it as a function
140 reference
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200141
142`:def` has no options like `:function` does: "range", "abort", "dict" or
Bram Moolenaar1b884a02020-12-10 21:11:27 +0100143"closure". A `:def` function always aborts on an error (unless `:silent!` was
144used for the command or inside a `:try` block), does not get a range passed
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100145cannot be a "dict" function, and can always be a closure.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200147The argument types and return type need to be specified. The "any" type can
148be used, type checking will then be done at runtime, like with legacy
149functions.
150
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200151Arguments are accessed by name, without "a:", just like any other language.
152There is no "a:" dictionary or "a:000" list.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200153
154Variable arguments are defined as the last argument, with a name and have a
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200155list type, similar to TypeScript. For example, a list of numbers: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200156 def MyFunc(...itemlist: list<number>)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 for item in itemlist
158 ...
159
160
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200161Functions and variables are script-local by default ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200162 *vim9-scopes*
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200163When using `:function` or `:def` to specify a new function at the script level
164in a Vim9 script, the function is local to the script, as if "s:" was
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200165prefixed. Using the "s:" prefix is optional. To define a global function or
166variable the "g:" prefix must be used. For functions in an autoload script
167the "name#" prefix is sufficient. >
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200168 def ThisFunction() # script-local
169 def s:ThisFunction() # script-local
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200170 def g:ThatFunction() # global
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200171 def scriptname#function() # autoload
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200172
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200173When using `:function` or `:def` to specify a nested function inside a `:def`
174function, this nested function is local to the code block it is defined in.
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +0200175In a `:def` function it is not possible to define a script-local function. It
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200176is possible to define a global function by using the "g:" prefix.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200177
178When referring to a function and no "s:" or "g:" prefix is used, Vim will
Bram Moolenaar13106602020-10-04 16:06:05 +0200179search for the function:
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +0200180- in the function scope, in block scopes
Bram Moolenaar13106602020-10-04 16:06:05 +0200181- in the script scope, possibly imported
182- in the list of global functions
183However, it is recommended to always use "g:" to refer to a global function
184for clarity.
185
186In all cases the function must be defined before used. That is when it is
Bram Moolenaarcb80aa22020-10-26 21:12:46 +0100187called, when `:defcompile` causes it to be compiled, or when code that calls
188it is being compiled (to figure out the return type).
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200189
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200190The result is that functions and variables without a namespace can usually be
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200191found in the script, either defined there or imported. Global functions and
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200192variables could be defined anywhere (good luck finding out where!).
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200193
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200194Global functions can still be defined and deleted at nearly any time. In
Bram Moolenaar2cfb4a22020-05-07 18:56:00 +0200195Vim9 script script-local functions are defined once when the script is sourced
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200196and cannot be deleted or replaced.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200197
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100198When compiling a function and a function call is encountered for a function
199that is not (yet) defined, the |FuncUndefined| autocommand is not triggered.
200You can use an autoload function if needed, or call a legacy function and have
201|FuncUndefined| triggered there.
202
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200203
Bram Moolenaar2b327002020-12-26 15:39:31 +0100204Reloading a Vim9 script clears functions and variables by default ~
205 *vim9-reload*
206When loading a legacy Vim script a second time nothing is removed, the
207commands will replace existing variables and functions and create new ones.
208
209When loading a Vim9 script a second time all existing script-local functions
210and variables are deleted, thus you start with a clean slate. This is useful
211if you are developing a plugin and want to try a new version. If you renamed
212something you don't have to worry about the old name still hanging around.
213
214If you do want to keep items, use: >
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100215 vim9script noclear
Bram Moolenaar2b327002020-12-26 15:39:31 +0100216
217You want to use this in scripts that use a `finish` command to bail out at
218some point when loaded again. E.g. when a buffer local option is set: >
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100219 vim9script noclear
Bram Moolenaar2b327002020-12-26 15:39:31 +0100220 setlocal completefunc=SomeFunc
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100221 if exists('*g:SomeFunc') | finish | endif
Bram Moolenaar2b327002020-12-26 15:39:31 +0100222 def g:SomeFunc()
223 ....
224
Bram Moolenaar2b327002020-12-26 15:39:31 +0100225
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200226Variable declarations with :var, :final and :const ~
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200227 *vim9-declaration* *:var*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200228Local variables need to be declared with `:var`. Local constants need to be
229declared with `:final` or `:const`. We refer to both as "variables" in this
230section.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100231
232Variables can be local to a script, function or code block: >
233 vim9script
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200234 var script_var = 123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 def SomeFunc()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200236 var func_var = script_var
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237 if cond
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200238 var block_var = func_var
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 ...
240
241The variables are only visible in the block where they are defined and nested
242blocks. Once the block ends the variable is no longer accessible: >
243 if cond
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200244 var inner = 5
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 else
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200246 var inner = 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 endif
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200248 echo inner # Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249
250The declaration must be done earlier: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200251 var inner: number
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 if cond
253 inner = 5
254 else
255 inner = 0
256 endif
257 echo inner
258
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200259To intentionally hide a variable from code that follows, a block can be
260used: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200262 var temp = 'temp'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263 ...
264 }
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200265 echo temp # Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200267Declaring a variable with a type but without an initializer will initialize to
268zero, false or empty.
269
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200270In Vim9 script `:let` cannot be used. An existing variable is assigned to
271without any command. The same for global, window, tab, buffer and Vim
272variables, because they are not really declared. They can also be deleted
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200273with `:unlet`.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200275Variables and functions cannot shadow previously defined or imported variables
276and functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277Variables may shadow Ex commands, rename the variable if needed.
278
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200279Global variables and user defined functions must be prefixed with "g:", also
280at the script level. >
Bram Moolenaard1caa942020-04-10 22:10:56 +0200281 vim9script
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200282 var script_local = 'text'
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200283 g:global = 'value'
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200284 var Funcref = g:ThatFunction
Bram Moolenaard1caa942020-04-10 22:10:56 +0200285
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200286Since `&opt = value` is now assigning a value to option "opt", ":&" cannot be
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100287used to repeat a `:substitute` command.
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200288
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200289
290Constants ~
291 *vim9-const* *vim9-final*
292How constants work varies between languages. Some consider a variable that
293can't be assigned another value a constant. JavaScript is an example. Others
294also make the value immutable, thus when a constant uses a list, the list
295cannot be changed. In Vim9 we can use both.
296
297`:const` is used for making both the variable and the value a constant. Use
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200298this for composite structures that you want to make sure will not be modified.
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200299Example: >
300 const myList = [1, 2]
301 myList = [3, 4] # Error!
302 myList[0] = 9 # Error!
303 muList->add(3) # Error!
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200304< *:final*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200305`:final` is used for making only the variable a constant, the value can be
306changed. This is well known from Java. Example: >
307 final myList = [1, 2]
308 myList = [3, 4] # Error!
309 myList[0] = 9 # OK
310 muList->add(3) # OK
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200311
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200312It is common to write constants as ALL_CAPS, but you don't have to.
313
314The constant only applies to the value itself, not what it refers to. >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200315 final females = ["Mary"]
316 const NAMES = [["John", "Peter"], females]
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200317 NAMES[0] = ["Jack"] # Error!
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200318 NAMES[0][0] = "Jack" # Error!
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200319 NAMES[1] = ["Emma"] # Error!
Bram Moolenaar82be4842021-01-11 19:40:15 +0100320 NAMES[1][0] = "Emma" # OK, now females[0] == "Emma"
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200321
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200322< *E1092*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200323Declaring more than one variable at a time, using the unpack notation, is
324currently not supported: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200325 var [v1, v2] = GetValues() # Error!
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200326That is because the type needs to be inferred from the list item type, which
327isn't that easy.
328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329
330Omitting :call and :eval ~
331
332Functions can be called without `:call`: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200333 writefile(lines, 'file')
Bram Moolenaar560979e2020-02-04 22:53:05 +0100334Using `:call` is still possible, but this is discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335
336A method call without `eval` is possible, so long as the start is an
Bram Moolenaarae616492020-07-28 20:07:27 +0200337identifier or can't be an Ex command. Examples: >
338 myList->add(123)
339 g:myList->add(123)
340 [1, 2, 3]->Process()
Bram Moolenaar2bede172020-11-19 18:53:18 +0100341 {a: 1, b: 2}->Process()
Bram Moolenaarae616492020-07-28 20:07:27 +0200342 "foobar"->Process()
343 ("foobar")->Process()
344 'foobar'->Process()
345 ('foobar')->Process()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200347In the rare case there is ambiguity between a function name and an Ex command,
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200348prepend ":" to make clear you want to use the Ex command. For example, there
349is both the `:substitute` command and the `substitute()` function. When the
350line starts with `substitute(` this will use the function. Prepend a colon to
351use the command instead: >
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100352 :substitute(pattern (replacement (
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100353
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100354Note that while variables need to be defined before they can be used,
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200355functions can be called before being defined. This is required to allow
356for cyclic dependencies between functions. It is slightly less efficient,
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100357since the function has to be looked up by name. And a typo in the function
Bram Moolenaarae616492020-07-28 20:07:27 +0200358name will only be found when the function is called.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360
Bram Moolenaard1caa942020-04-10 22:10:56 +0200361Omitting function() ~
362
363A user defined function can be used as a function reference in an expression
364without `function()`. The argument types and return type will then be checked.
365The function must already have been defined. >
366
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200367 var Funcref = MyFunction
Bram Moolenaard1caa942020-04-10 22:10:56 +0200368
369When using `function()` the resulting type is "func", a function with any
370number of arguments and any return type. The function can be defined later.
371
372
Bram Moolenaar2b327002020-12-26 15:39:31 +0100373Lambda using => instead of -> ~
Bram Moolenaar65c44152020-12-24 15:14:01 +0100374
375In legacy script there can be confusion between using "->" for a method call
376and for a lambda. Also, when a "{" is found the parser needs to figure out if
377it is the start of a lambda or a dictionary, which is now more complicated
378because of the use of argument types.
379
380To avoid these problems Vim9 script uses a different syntax for a lambda,
381which is similar to Javascript: >
382 var Lambda = (arg) => expression
383
Bram Moolenaar2b327002020-12-26 15:39:31 +0100384No line break is allowed in the arguments of a lambda up to and including the
Bram Moolenaar65c44152020-12-24 15:14:01 +0100385"=>". This is OK: >
386 filter(list, (k, v) =>
387 v > 0)
388This does not work: >
389 filter(list, (k, v)
390 => v > 0)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100391This also does not work: >
Bram Moolenaar65c44152020-12-24 15:14:01 +0100392 filter(list, (k,
393 v) => v > 0)
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100394But you can use a backslash to concatenate the lines before parsing: >
395 filter(list, (k,
396 \ v)
397 \ => v > 0)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100398
399Additionally, a lambda can contain statements in {}: >
400 var Lambda = (arg) => {
401 g:was_called = 'yes'
402 return expression
403 }
404NOT IMPLEMENTED YET
405
Bram Moolenaare0e39172021-01-25 21:14:57 +0100406 *vim9-curly*
Bram Moolenaar2b327002020-12-26 15:39:31 +0100407To avoid the "{" of a dictionary literal to be recognized as a statement block
408wrap it in parenthesis: >
409 var Lambda = (arg) => ({key: 42})
Bram Moolenaar65c44152020-12-24 15:14:01 +0100410
Bram Moolenaare0e39172021-01-25 21:14:57 +0100411Also when confused with the start of a command block: >
412 ({
413 key: value
414 })->method()
415
Bram Moolenaar65c44152020-12-24 15:14:01 +0100416
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200417Automatic line continuation ~
418
419In many cases it is obvious that an expression continues on the next line. In
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100420those cases there is no need to prefix the line with a backslash (see
421|line-continuation|). For example, when a list spans multiple lines: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200422 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200423 'one',
424 'two',
425 ]
Bram Moolenaare6085c52020-04-12 20:19:16 +0200426And when a dict spans multiple lines: >
Bram Moolenaar2bede172020-11-19 18:53:18 +0100427 var mydict = {
Bram Moolenaare6085c52020-04-12 20:19:16 +0200428 one: 1,
429 two: 2,
430 }
431Function call: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200432 var result = Func(
Bram Moolenaare6085c52020-04-12 20:19:16 +0200433 arg1,
434 arg2
435 )
436
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200437For binary operators in expressions not in [], {} or () a line break is
438possible just before or after the operator. For example: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200439 var text = lead
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200440 .. middle
441 .. end
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200442 var total = start +
Bram Moolenaar82be4842021-01-11 19:40:15 +0100443 end -
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200444 correction
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200445 var result = positive
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200446 ? PosFunc(arg)
447 : NegFunc(arg)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200448
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200449For a method call using "->" and a member using a dot, a line break is allowed
450before it: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200451 var result = GetBuilder()
Bram Moolenaar73fef332020-06-21 22:12:03 +0200452 ->BuilderSetWidth(333)
453 ->BuilderSetHeight(777)
454 ->BuilderBuild()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200455 var result = MyDict
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200456 .member
Bram Moolenaar73fef332020-06-21 22:12:03 +0200457
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100458For commands that have an argument that is a list of commands, the | character
459at the start of the line indicates line continuation: >
460 autocmd BufNewFile *.match if condition
461 | echo 'match'
462 | endif
463
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200464< *E1050*
465To make it possible for the operator at the start of the line to be
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200466recognized, it is required to put a colon before a range. This will add
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200467"start" and print: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200468 var result = start
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200469 + print
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200470Like this: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200471 var result = start + print
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200472
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200473This will assign "start" and print a line: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200474 var result = start
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200475 :+ print
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200476
Bram Moolenaar23515b42020-11-29 14:36:24 +0100477Note that the colon is not required for the |+cmd| argument: >
478 edit +6 fname
479
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200480It is also possible to split a function header over multiple lines, in between
481arguments: >
482 def MyFunc(
483 text: string,
484 separator = '-'
485 ): string
486
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100487Since a continuation line cannot be easily recognized the parsing of commands
Bram Moolenaar65c44152020-12-24 15:14:01 +0100488has been made stricter. E.g., because of the error in the first line, the
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100489second line is seen as a separate command: >
490 popup_create(some invalid expression, {
491 exit_cb: Func})
492Now "exit_cb: Func})" is actually a valid command: save any changes to the
493file "_cb: Func})" and exit. To avoid this kind of mistake in Vim9 script
494there must be white space between most command names and the argument.
495
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100496However, the argument of a command that is a command won't be recognized. For
497example, after "windo echo expr" a line break inside "expr" will not be seen.
498
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100499
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200500Notes:
501- "enddef" cannot be used at the start of a continuation line, it ends the
502 current function.
503- No line break is allowed in the LHS of an assignment. Specifically when
504 unpacking a list |:let-unpack|. This is OK: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200505 [var1, var2] =
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200506 Func()
507< This does not work: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200508 [var1,
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200509 var2] =
510 Func()
511- No line break is allowed in between arguments of an `:echo`, `:execute` and
512 similar commands. This is OK: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200513 echo [1,
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200514 2] [3,
515 4]
516< This does not work: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200517 echo [1, 2]
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200518 [3, 4]
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100520No curly braces expansion ~
521
522|curly-braces-names| cannot be used.
523
524
Bram Moolenaar2bede172020-11-19 18:53:18 +0100525Dictionary literals ~
526
527Traditionally Vim has supported dictionary literals with a {} syntax: >
528 let dict = {'key': value}
529
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100530Later it became clear that using a simple text key is very common, thus
531literal dictionaries were introduced in a backwards compatible way: >
Bram Moolenaar2bede172020-11-19 18:53:18 +0100532 let dict = #{key: value}
533
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100534However, this #{} syntax is unlike any existing language. As it turns out
535that using a literal key is much more common than using an expression, and
Bram Moolenaar2bede172020-11-19 18:53:18 +0100536considering that JavaScript uses this syntax, using the {} form for dictionary
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100537literals is considered a much more useful syntax. In Vim9 script the {} form
Bram Moolenaar2bede172020-11-19 18:53:18 +0100538uses literal keys: >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100539 var dict = {key: value}
Bram Moolenaar2bede172020-11-19 18:53:18 +0100540
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100541This works for alphanumeric characters, underscore and dash. If you want to
542use another character, use a single or double quoted string: >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100543 var dict = {'key with space': value}
544 var dict = {"key\twith\ttabs": value}
545 var dict = {'': value} # empty key
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100546
547In case the key needs to be an expression, square brackets can be used, just
548like in JavaScript: >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100549 var dict = {["key" .. nr]: value}
Bram Moolenaar2bede172020-11-19 18:53:18 +0100550
551
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200552No :xit, :t, :append, :change or :insert ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200554These commands are too easily confused with local variable names.
555Instead of `:x` or `:xit` you can use `:exit`.
556Instead of `:t` you can use `:copy`.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100557
558
559Comparators ~
560
561The 'ignorecase' option is not used for comparators that use strings.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562
563
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100564For loop ~
565
566Legacy Vim script has some tricks to make a for loop over a list handle
567deleting items at the current or previous item. In Vim9 script it just uses
568the index, if items are deleted then items in the list will be skipped.
569Example legacy script: >
570 let l = [1, 2, 3, 4]
571 for i in l
572 echo i
573 call remove(l, index(l, i))
574 endfor
575Would echo:
576 1
577 2
578 3
579 4
580In compiled Vim9 script you get:
581 1
582 3
583Generally, you should not change the list that is iterated over. Make a copy
584first if needed.
585
586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587White space ~
588
589Vim9 script enforces proper use of white space. This is no longer allowed: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200590 var name=234 # Error!
591 var name= 234 # Error!
592 var name =234 # Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593There must be white space before and after the "=": >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200594 var name = 234 # OK
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200595White space must also be put before the # that starts a comment after a
596command: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200597 var name = 234# Error!
598 var name = 234 # OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599
600White space is required around most operators.
601
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100602White space is required in a sublist (list slice) around the ":", except at
603the start and end: >
604 otherlist = mylist[v : count] # v:count has a different meaning
605 otherlist = mylist[:] # make a copy of the List
606 otherlist = mylist[v :]
607 otherlist = mylist[: v]
608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609White space is not allowed:
610- Between a function name and the "(": >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100611 Func (arg) # Error!
612 Func
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200613 \ (arg) # Error!
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100614 Func
615 (arg) # Error!
616 Func(arg) # OK
617 Func(
618 arg) # OK
619 Func(
620 arg # OK
621 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622
623
624Conditions and expressions ~
625
Bram Moolenaar13106602020-10-04 16:06:05 +0200626Conditions and expressions are mostly working like they do in other languages.
627Some values are different from legacy Vim script:
628 value legacy Vim script Vim9 script ~
629 0 falsy falsy
630 1 truthy truthy
631 99 truthy Error!
632 "0" falsy Error!
633 "99" truthy Error!
634 "text" falsy Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635
Bram Moolenaar13106602020-10-04 16:06:05 +0200636For the "??" operator and when using "!" then there is no error, every value
637is either falsy or truthy. This is mostly like JavaScript, except that an
638empty list and dict is falsy:
639
640 type truthy when ~
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100641 bool true, v:true or 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 number non-zero
643 float non-zero
644 string non-empty
645 blob non-empty
646 list non-empty (different from JavaScript)
647 dictionary non-empty (different from JavaScript)
Bram Moolenaard1caa942020-04-10 22:10:56 +0200648 func when there is a function name
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100649 special true or v:true
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650 job when not NULL
651 channel when not NULL
652 class when not NULL
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100653 object when not NULL (TODO: when isTrue() returns true)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200655The boolean operators "||" and "&&" expect the values to be boolean, zero or
656one: >
657 1 || false == true
658 0 || 1 == true
659 0 || false == false
660 1 && true == true
661 0 && 1 == false
662 8 || 0 Error!
663 'yes' && 0 Error!
664 [] || 99 Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200666When using "!" for inverting, there is no error for using any type and the
Bram Moolenaar13106602020-10-04 16:06:05 +0200667result is a boolean. "!!" can be used to turn any value into boolean: >
Bram Moolenaar82be4842021-01-11 19:40:15 +0100668 !'yes' == false
Bram Moolenaar13106602020-10-04 16:06:05 +0200669 !![] == false
Bram Moolenaar82be4842021-01-11 19:40:15 +0100670 !![1, 2, 3] == true
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200671
672When using "`.."` for string concatenation arguments of simple types are
Bram Moolenaar13106602020-10-04 16:06:05 +0200673always converted to string: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 'hello ' .. 123 == 'hello 123'
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100675 'hello ' .. v:true == 'hello true'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200677Simple types are string, float, special and bool. For other types |string()|
678can be used.
Bram Moolenaar67977822021-01-03 21:53:53 +0100679 *false* *true* *null*
680In Vim9 script one can use "true" for v:true, "false" for v:false and "null"
681for v:null. When converting a boolean to a string "false" and "true" are
682used, not "v:false" and "v:true" like in legacy script. "v:none" is not
683changed, it is only used in JSON and has no equivalent in other languages.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100685Indexing a string with [idx] or [idx : idx] uses character indexes instead of
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200686byte indexes. Example: >
687 echo 'bár'[1]
688In legacy script this results in the character 0xc3 (an illegal byte), in Vim9
689script this results in the string 'á'.
Bram Moolenaar82be4842021-01-11 19:40:15 +0100690A negative index is counting from the end, "[-1]" is the last character.
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100691To exclude the last character use |slice()|.
Bram Moolenaar82be4842021-01-11 19:40:15 +0100692If the index is out of range then an empty string results.
693
694In legacy script "++var" and "--var" would be silently accepted and have no
695effect. This is an error in Vim9 script.
696
697Numbers starting with zero are not considered to be octal, only numbers
698starting with "0o" are octal: "0o744". |scriptversion-4|
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700
Bram Moolenaare46a4402020-06-30 20:38:27 +0200701What to watch out for ~
702 *vim9-gotchas*
703Vim9 was designed to be closer to often used programming languages, but at the
704same time tries to support the legacy Vim commands. Some compromises had to
705be made. Here is a summary of what might be unexpected.
706
707Ex command ranges need to be prefixed with a colon. >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100708 -> legacy Vim: shifts the previous line to the right
709 ->func() Vim9: method call in a continuation line
710 :-> Vim9: shifts the previous line to the right
Bram Moolenaare46a4402020-06-30 20:38:27 +0200711
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100712 %s/a/b legacy Vim: substitute on all lines
Bram Moolenaare46a4402020-06-30 20:38:27 +0200713 x = alongname
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100714 % another Vim9: modulo operator in a continuation line
715 :%s/a/b Vim9: substitute on all lines
716 't legacy Vim: jump to mark t
717 'text'->func() Vim9: method call
718 :'t Vim9: jump to mark t
Bram Moolenaare46a4402020-06-30 20:38:27 +0200719
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200720Some Ex commands can be confused with assignments in Vim9 script: >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100721 g:name = value # assignment
722 g:pattern:cmd # invalid command - ERROR
723 :g:pattern:cmd # :global command
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200724
Bram Moolenaare46a4402020-06-30 20:38:27 +0200725Functions defined with `:def` compile the whole function. Legacy functions
726can bail out, and the following lines are not parsed: >
727 func Maybe()
728 if !has('feature')
729 return
730 endif
731 use-feature
732 endfunc
733Vim9 functions are compiled as a whole: >
734 def Maybe()
735 if !has('feature')
736 return
737 endif
Bram Moolenaar82be4842021-01-11 19:40:15 +0100738 use-feature # May give a compilation error
Bram Moolenaare46a4402020-06-30 20:38:27 +0200739 enddef
740For a workaround, split it in two functions: >
741 func Maybe()
742 if has('feature')
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100743 call MaybeInner()
Bram Moolenaare46a4402020-06-30 20:38:27 +0200744 endif
745 endfunc
746 if has('feature')
747 def MaybeInner()
748 use-feature
749 enddef
750 endif
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200751Or put the unsupported code inside an `if` with a constant expression that
Bram Moolenaar207f0092020-08-30 17:20:20 +0200752evaluates to false: >
753 def Maybe()
754 if has('feature')
755 use-feature
756 endif
757 enddef
Bram Moolenaar82be4842021-01-11 19:40:15 +0100758< *vim9-user-command*
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100759Another side effect of compiling a function is that the presence of a user
Bram Moolenaar82be4842021-01-11 19:40:15 +0100760command is checked at compile time. If the user command is defined later an
761error will result. This works: >
762 command -nargs=1 MyCommand echom <q-args>
763 def Works()
764 MyCommand 123
765 enddef
766This will give an error for "MyCommand" not being defined: >
767 def Works()
768 command -nargs=1 MyCommand echom <q-args>
769 MyCommand 123
770 enddef
771A workaround is to invoke the command indirectly with `:execute`: >
772 def Works()
773 command -nargs=1 MyCommand echom <q-args>
774 execute 'MyCommand 123'
775 enddef
776
Bram Moolenaar207f0092020-08-30 17:20:20 +0200777Note that for unrecognized commands there is no check for "|" and a following
778command. This will give an error for missing `endif`: >
779 def Maybe()
780 if has('feature') | use-feature | endif
781 enddef
Bram Moolenaare46a4402020-06-30 20:38:27 +0200782
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100783Other differences ~
784
785Patterns are used like 'magic' is set, unless explicitly overruled.
786The 'edcompatible' option value is not used.
787The 'gdefault' option value is not used.
788
789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790==============================================================================
791
7923. New style functions *fast-functions*
793
794THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
795
796 *:def*
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200797:def[!] {name}([arguments])[: {return-type}]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 Define a new function by the name {name}. The body of
799 the function follows in the next lines, until the
800 matching `:enddef`.
801
Bram Moolenaard77a8522020-04-03 21:59:57 +0200802 When {return-type} is omitted or is "void" the
803 function is not expected to return anything.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804
805 {arguments} is a sequence of zero or more argument
806 declarations. There are three forms:
807 {name}: {type}
808 {name} = {value}
809 {name}: {type} = {value}
810 The first form is a mandatory argument, the caller
811 must always provide them.
812 The second and third form are optional arguments.
813 When the caller omits an argument the {value} is used.
814
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200815 The function will be compiled into instructions when
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200816 called, or when `:disassemble` or `:defcompile` is
817 used. Syntax and type errors will be produced at that
818 time.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200819
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200820 It is possible to nest `:def` inside another `:def` or
821 `:function` up to about 50 levels deep.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100822
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200823 [!] is used as with `:function`. Note that
824 script-local functions cannot be deleted or redefined
825 later in Vim9 script. They can only be removed by
826 reloading the same script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827
828 *:enddef*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200829:enddef End of a function defined with `:def`. It should be on
830 a line by its own.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831
832
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100833If the script the function is defined in is Vim9 script, then script-local
834variables can be accessed without the "s:" prefix. They must be defined
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200835before the function is compiled. If the script the function is defined in is
836legacy script, then script-local variables must be accessed with the "s:"
Bram Moolenaar207f0092020-08-30 17:20:20 +0200837prefix and they do not need to exist (they can be deleted any time).
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100838
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200839 *:defc* *:defcompile*
840:defc[ompile] Compile functions defined in the current script that
841 were not compiled yet.
842 This will report errors found during the compilation.
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100843
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100844 *:disa* *:disassemble*
845:disa[ssemble] {func} Show the instructions generated for {func}.
846 This is for debugging and testing.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100847 Note that for command line completion of {func} you
848 can prepend "s:" to find script-local functions.
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100849
Bram Moolenaare0e39172021-01-25 21:14:57 +0100850:disa[ssemble]! {func} Like `:disassemble` but with the instructions used for
851 profiling.
852
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200853Limitations ~
854
855Local variables will not be visible to string evaluation. For example: >
Bram Moolenaar2b327002020-12-26 15:39:31 +0100856 def MapList(): list<string>
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200857 var list = ['aa', 'bb', 'cc', 'dd']
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200858 return range(1, 2)->map('list[v:val]')
859 enddef
860
861The map argument is a string expression, which is evaluated without the
862function scope. Instead, use a lambda: >
Bram Moolenaar2b327002020-12-26 15:39:31 +0100863 def MapList(): list<string>
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200864 var list = ['aa', 'bb', 'cc', 'dd']
Bram Moolenaar2b327002020-12-26 15:39:31 +0100865 return range(1, 2)->map(( _, v) => list[v])
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200866 enddef
867
Bram Moolenaar2b327002020-12-26 15:39:31 +0100868The same is true for commands that are not compiled, such as `:global`.
869For these the backtick expansion can be used. Example: >
870 def Replace()
871 var newText = 'blah'
872 g/pattern/s/^/`=newText`/
873 enddef
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875==============================================================================
876
8774. Types *vim9-types*
878
879THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
880
881The following builtin types are supported:
882 bool
883 number
884 float
885 string
886 blob
Bram Moolenaard77a8522020-04-03 21:59:57 +0200887 list<{type}>
888 dict<{type}>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 job
890 channel
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100891 func
Bram Moolenaard1caa942020-04-10 22:10:56 +0200892 func: {type}
Bram Moolenaard77a8522020-04-03 21:59:57 +0200893 func({type}, ...)
894 func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895
896Not supported yet:
Bram Moolenaard77a8522020-04-03 21:59:57 +0200897 tuple<a: {type}, b: {type}, ...>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
Bram Moolenaard77a8522020-04-03 21:59:57 +0200899These types can be used in declarations, but no value will have this type:
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200900 {type}|{type} {not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 void
902 any
903
Bram Moolenaard77a8522020-04-03 21:59:57 +0200904There is no array type, use list<{type}> instead. For a list constant an
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905efficient implementation is used that avoids allocating lot of small pieces of
906memory.
907
Bram Moolenaard77a8522020-04-03 21:59:57 +0200908A partial and function can be declared in more or less specific ways:
909func any kind of function reference, no type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200910 checking for arguments or return value
Bram Moolenaard77a8522020-04-03 21:59:57 +0200911func: {type} any number and type of arguments with specific
912 return type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200913func({type}) function with argument type, does not return
Bram Moolenaard77a8522020-04-03 21:59:57 +0200914 a value
Bram Moolenaard1caa942020-04-10 22:10:56 +0200915func({type}): {type} function with argument type and return type
916func(?{type}) function with type of optional argument, does
917 not return a value
918func(...{type}) function with type of variable number of
919 arguments, does not return a value
920func({type}, ?{type}, ...{type}): {type}
921 function with:
922 - type of mandatory argument
923 - type of optional argument
924 - type of variable number of arguments
925 - return type
Bram Moolenaard77a8522020-04-03 21:59:57 +0200926
927If the return type is "void" the function does not return a value.
928
929The reference can also be a |Partial|, in which case it stores extra arguments
930and/or a dictionary, which are not visible to the caller. Since they are
931called in the same way the declaration is the same.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932
933Custom types can be defined with `:type`: >
934 :type MyList list<string>
Bram Moolenaar127542b2020-08-09 17:22:04 +0200935Custom types must start with a capital letter, to avoid name clashes with
936builtin types added later, similarly to user functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937{not implemented yet}
938
939And classes and interfaces can be used as types: >
940 :class MyClass
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200941 :var mine: MyClass
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942
943 :interface MyInterface
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200944 :var mine: MyInterface
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945
946 :class MyTemplate<Targ>
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200947 :var mine: MyTemplate<number>
948 :var mine: MyTemplate<string>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 :class MyInterface<Targ>
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200951 :var mine: MyInterface<number>
952 :var mine: MyInterface<string>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953{not implemented yet}
954
955
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200956Variable types and type casting ~
957 *variable-types*
Bram Moolenaar64d662d2020-08-09 19:02:50 +0200958Variables declared in Vim9 script or in a `:def` function have a type, either
959specified explicitly or inferred from the initialization.
960
961Global, buffer, window and tab page variables do not have a specific type, the
962value can be changed at any time, possibly changing the type. Therefore, in
963compiled code the "any" type is assumed.
964
965This can be a problem when the "any" type is undesired and the actual type is
966expected to always be the same. For example, when declaring a list: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200967 var l: list<number> = [1, g:two]
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100968At compile time Vim doesn't know the type of "g:two" and the expression type
969becomes list<any>. An instruction is generated to check the list type before
970doing the assignment, which is a bit inefficient.
971 *type-casting*
972To avoid this, use a type cast: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200973 var l: list<number> = [1, <number>g:two]
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100974The compiled code will then only check that "g:two" is a number and give an
975error if it isn't. This is called type casting.
Bram Moolenaar64d662d2020-08-09 19:02:50 +0200976
977The syntax of a type cast is: "<" {type} ">". There cannot be white space
978after the "<" or before the ">" (to avoid them being confused with
979smaller-than and bigger-than operators).
980
981The semantics is that, if needed, a runtime type check is performed. The
982value is not actually changed. If you need to change the type, e.g. to change
983it to a string, use the |string()| function. Or use |str2nr()| to convert a
984string to a number.
985
986
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200987Type inference ~
988 *type-inference*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989In general: Whenever the type is clear it can be omitted. For example, when
990declaring a variable and giving it a value: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200991 var name = 0 # infers number type
992 var name = 'hello' # infers string type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
Bram Moolenaar127542b2020-08-09 17:22:04 +0200994The type of a list and dictionary comes from the common type of the values.
995If the values all have the same type, that type is used for the list or
996dictionary. If there is a mix of types, the "any" type is used. >
997 [1, 2, 3] list<number>
998 ['a', 'b', 'c'] list<string>
999 [1, 'x', 3] list<any>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000
Bram Moolenaar207f0092020-08-30 17:20:20 +02001001
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001002Stricter type checking ~
1003 *type-checking*
Bram Moolenaar207f0092020-08-30 17:20:20 +02001004In legacy Vim script, where a number was expected, a string would be
1005automatically converted to a number. This was convenient for an actual number
1006such as "123", but leads to unexpected problems (but no error message) if the
1007string doesn't start with a number. Quite often this leads to hard-to-find
1008bugs.
1009
1010In Vim9 script this has been made stricter. In most places it works just as
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001011before, if the value used matches the expected type. There will sometimes be
1012an error, thus breaking backwards compatibility. For example:
Bram Moolenaar207f0092020-08-30 17:20:20 +02001013- Using a number other than 0 or 1 where a boolean is expected. *E1023*
1014- Using a string value when setting a number options.
1015- Using a number where a string is expected. *E1024*
1016
Bram Moolenaar82be4842021-01-11 19:40:15 +01001017One consequence is that the item type of a list or dict given to map() must
1018not change. This will give an error in compiled code: >
1019 map([1, 2, 3], (i, v) => 'item ' .. i)
1020 E1012: Type mismatch; expected list<number> but got list<string>
1021Instead use |mapnew()|.
1022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023==============================================================================
1024
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010255. Namespace, Import and Export
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 *vim9script* *vim9-export* *vim9-import*
1027
1028THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
1029
1030A Vim9 script can be written to be imported. This means that everything in
1031the script is local, unless exported. Those exported items, and only those
1032items, can then be imported in another script.
1033
Bram Moolenaar207f0092020-08-30 17:20:20 +02001034You can cheat by using the global namespace explicitly. We will assume here
1035that you don't do that.
1036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037
1038Namespace ~
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001039 *vim9-namespace*
Bram Moolenaar560979e2020-02-04 22:53:05 +01001040To recognize a file that can be imported the `vim9script` statement must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041appear as the first statement in the file. It tells Vim to interpret the
1042script in its own namespace, instead of the global namespace. If a file
1043starts with: >
1044 vim9script
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001045 var myvar = 'yes'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046Then "myvar" will only exist in this file. While without `vim9script` it would
1047be available as `g:myvar` from any other script and function.
1048
1049The variables at the file level are very much like the script-local "s:"
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +02001050variables in legacy Vim script, but the "s:" is omitted. And they cannot be
1051deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +02001053In Vim9 script the global "g:" namespace can still be used as before. And the
1054"w:", "b:" and "t:" namespaces. These have in common that variables are not
1055declared and they can be deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056
1057A side effect of `:vim9script` is that the 'cpoptions' option is set to the
1058Vim default value, like with: >
1059 :set cpo&vim
1060One of the effects is that |line-continuation| is always enabled.
1061The original value of 'cpoptions' is restored at the end of the script.
1062
1063
1064Export ~
1065 *:export* *:exp*
Bram Moolenaar2547aa92020-07-26 17:00:44 +02001066Exporting an item can be written as: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 export const EXPORTED_CONST = 1234
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001068 export var someValue = ...
1069 export final someValue = ...
1070 export const someValue = ...
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 export def MyFunc() ...
1072 export class MyClass ...
1073
1074As this suggests, only constants, variables, `:def` functions and classes can
Bram Moolenaar2547aa92020-07-26 17:00:44 +02001075be exported. {classes are not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001077 *E1042*
1078`:export` can only be used in Vim9 script, at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079
1080
1081Import ~
Bram Moolenaar73fef332020-06-21 22:12:03 +02001082 *:import* *:imp* *E1094*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083The exported items can be imported individually in another Vim9 script: >
1084 import EXPORTED_CONST from "thatscript.vim"
1085 import MyClass from "myclass.vim"
1086
1087To import multiple items at the same time: >
1088 import {someValue, MyClass} from "thatscript.vim"
1089
Bram Moolenaar560979e2020-02-04 22:53:05 +01001090In case the name is ambiguous, another name can be specified: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 import MyClass as ThatClass from "myclass.vim"
1092 import {someValue, MyClass as ThatClass} from "myclass.vim"
1093
1094To import all exported items under a specific identifier: >
1095 import * as That from 'thatscript.vim'
1096
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001097{not implemented yet: using "This as That"}
1098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free
1100to choose the name "That", but it is highly recommended to use the name of the
1101script file to avoid confusion.
1102
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001103`:import` can also be used in legacy Vim script. The imported items still
1104become script-local, even when the "s:" prefix is not given.
1105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106The script name after `import` can be:
1107- A relative path, starting "." or "..". This finds a file relative to the
1108 location of the script file itself. This is useful to split up a large
1109 plugin into several files.
1110- An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001111 will rarely be used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112- A path not being relative or absolute. This will be found in the
1113 "import" subdirectories of 'runtimepath' entries. The name will usually be
1114 longer and unique, to avoid loading the wrong file.
1115
1116Once a vim9 script file has been imported, the result is cached and used the
1117next time the same script is imported. It will not be read again.
1118 *:import-cycle*
1119The `import` commands are executed when encountered. If that script (directly
1120or indirectly) imports the current script, then items defined after the
1121`import` won't be processed yet. Therefore cyclic imports can exist, but may
1122result in undefined items.
1123
1124
1125Import in an autoload script ~
1126
1127For optimal startup speed, loading scripts should be postponed until they are
Bram Moolenaar560979e2020-02-04 22:53:05 +01001128actually needed. A recommended mechanism:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129
11301. In the plugin define user commands, functions and/or mappings that refer to
1131 an autoload script. >
Bram Moolenaar98a29d02021-01-18 19:55:44 +01001132 command -nargs=1 SearchForStuff searchfor#Stuff(<f-args>)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133
1134< This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen.
1135
Bram Moolenaar3d1cde82020-08-15 18:55:18 +020011362. In the autoload script do the actual work. You can import items from
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 other files to split up functionality in appropriate pieces. >
1138 vim9script
Bram Moolenaar82be4842021-01-11 19:40:15 +01001139 import FilterFunc from "../import/someother.vim"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 def searchfor#Stuff(arg: string)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001141 var filtered = FilterFunc(arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 ...
1143< This goes in .../autoload/searchfor.vim. "searchfor" in the file name
1144 must be exactly the same as the prefix for the function name, that is how
1145 Vim finds the file.
1146
11473. Other functionality, possibly shared between plugins, contains the exported
1148 items and any private items. >
1149 vim9script
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001150 var localVar = 'local'
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001151 export def FilterFunc(arg: string): string
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 ...
1153< This goes in .../import/someother.vim.
1154
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001155When compiling a `:def` function and a function in an autoload script is
1156encountered, the script is not loaded until the `:def` function is called.
1157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158
1159Import in legacy Vim script ~
1160
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001161If an `import` statement is used in legacy Vim script, the script-local "s:"
1162namespace will be used for the imported item, even when "s:" is not specified.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163
1164
1165==============================================================================
1166
Bram Moolenaar1d59aa12020-09-19 18:50:13 +020011676. Future work: classes *vim9-classes*
1168
1169Above "class" was mentioned a few times, but it has not been implemented yet.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001170Most of Vim9 script can be created without this functionality, and since
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001171implementing classes is going to be a lot of work, it is left for the future.
1172For now we'll just make sure classes can be added later.
1173
1174Thoughts:
1175- `class` / `endclass`, everything in one file
1176- Class names are always CamelCase
1177- Single constructor
1178- Single inheritance with `class ThisClass extends BaseClass`
1179- `abstract class`
1180- `interface` (Abstract class without any implementation)
1181- `class SomeClass implements SomeInterface`
1182- Generics for class: `class <Tkey, Tentry>`
1183- Generics for function: `def <Tkey> GetLast(key: Tkey)`
1184
1185Again, much of this is from TypeScript.
1186
1187Some things that look like good additions:
1188- Use a class as an interface (like Dart)
1189- Extend a class with methods, using an import (like Dart)
1190
1191An important class that will be provided is "Promise". Since Vim is single
1192threaded, connecting asynchronous operations is a natural way of allowing
1193plugins to do their work without blocking the user. It's a uniform way to
1194invoke callbacks and handle timeouts and errors.
1195
1196==============================================================================
1197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010011989. Rationale *vim9-rationale*
1199
1200The :def command ~
1201
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001202Plugin writers have asked for much faster Vim script. Investigations have
Bram Moolenaar560979e2020-02-04 22:53:05 +01001203shown that keeping the existing semantics of function calls make this close to
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204impossible, because of the overhead involved with calling a function, setting
1205up the local function scope and executing lines. There are many details that
1206need to be handled, such as error messages and exceptions. The need to create
1207a dictionary for a: and l: scopes, the a:000 list and several others add too
1208much overhead that cannot be avoided.
1209
1210Therefore the `:def` method to define a new-style function had to be added,
1211which allows for a function with different semantics. Most things still work
1212as before, but some parts do not. A new way to define a function was
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001213considered the best way to separate the legacy style code from Vim9 style code.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214
1215Using "def" to define a function comes from Python. Other languages use
1216"function" which clashes with legacy Vim script.
1217
1218
1219Type checking ~
1220
1221When compiling lines of Vim commands into instructions as much as possible
1222should be done at compile time. Postponing it to runtime makes the execution
1223slower and means mistakes are found only later. For example, when
1224encountering the "+" character and compiling this into a generic add
Bram Moolenaar98a29d02021-01-18 19:55:44 +01001225instruction, at runtime the instruction would have to inspect the type of the
1226arguments and decide what kind of addition to do. And when the type is
1227dictionary throw an error. If the types are known to be numbers then an "add
1228number" instruction can be used, which is faster. The error can be given at
1229compile time, no error handling is needed at runtime, since adding two numbers
1230cannot fail.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001232The syntax for types, using <type> for compound types, is similar to Java. It
1233is easy to understand and widely used. The type names are what were used in
1234Vim before, with some additions such as "void" and "bool".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235
1236
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001237Removing clutter and weirdness ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001238
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001239Once decided that `:def` functions have different syntax than legacy functions,
1240we are free to add improvements to make the code more familiar for users who
1241know popular programming languages. In other words: remove weird things that
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001242only Vim does.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001243
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001244We can also remove clutter, mainly things that were done to make Vim script
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001245backwards compatible with the good old Vi commands.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001246
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001247Examples:
1248- Drop `:call` for calling a function and `:eval` for manipulating data.
1249- Drop using a leading backslash for line continuation, automatically figure
1250 out where an expression ends.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001251
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001252However, this does require that some things need to change:
1253- Comments start with # instead of ", to avoid confusing them with strings.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001254 This is good anyway, it is known from several popular languages.
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001255- Ex command ranges need to be prefixed with a colon, to avoid confusion with
1256 expressions (single quote can be a string or a mark, "/" can be divide or a
1257 search command, etc.).
1258
1259Goal is to limit the differences. A good criteria is that when the old syntax
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001260is accidentally used you are very likely to get an error message.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001261
1262
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001263Syntax and semantics from popular languages ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265Script writers have complained that the Vim script syntax is unexpectedly
1266different from what they are used to. To reduce this complaint popular
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001267languages are used as an example. At the same time, we do not want to abandon
1268the well-known parts of legacy Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001270For many things TypeScript is followed. It's a recent language that is
1271gaining popularity and has similarities with Vim script. It also has a
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001272mix of static typing (a variable always has a known value type) and dynamic
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001273typing (a variable can have different types, this changes at runtime). Since
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001274legacy Vim script is dynamically typed and a lot of existing functionality
1275(esp. builtin functions) depends on that, while static typing allows for much
1276faster execution, we need to have this mix in Vim9 script.
1277
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001278There is no intention to completely match TypeScript syntax and semantics. We
1279just want to take those parts that we can use for Vim and we expect Vim users
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001280will be happy with. TypeScript is a complex language with its own history,
1281advantages and disadvantages. To get an idea of the disadvantages read the
1282book: "JavaScript: The Good Parts". Or find the article "TypeScript: the good
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001283parts" and read the "Things to avoid" section.
1284
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001285People familiar with other languages (Java, Python, etc.) will also find
1286things in TypeScript that they do not like or do not understand. We'll try to
1287avoid those things.
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001288
1289Specific items from TypeScript we avoid:
1290- Overloading "+", using it both for addition and string concatenation. This
1291 goes against legacy Vim script and often leads to mistakes. For that reason
1292 we will keep using ".." for string concatenation. Lua also uses ".." this
1293 way. And it allows for conversion to string for more values.
1294- TypeScript can use an expression like "99 || 'yes'" in a condition, but
1295 cannot assign the value to a boolean. That is inconsistent and can be
1296 annoying. Vim recognizes an expression with && or || and allows using the
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001297 result as a bool. TODO: to be reconsidered
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001298- TypeScript considers an empty string as Falsy, but an empty list or dict as
1299 Truthy. That is inconsistent. In Vim an empty list and dict are also
1300 Falsy.
1301- TypeScript has various "Readonly" types, which have limited usefulness,
1302 since a type cast can remove the immutable nature. Vim locks the value,
1303 which is more flexible, but is only checked at runtime.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001306Declarations ~
1307
1308Legacy Vim script uses `:let` for every assignment, while in Vim9 declarations
1309are used. That is different, thus it's good to use a different command:
1310`:var`. This is used in many languages. The semantics might be slightly
1311different, but it's easily recognized as a declaration.
1312
Bram Moolenaar23515b42020-11-29 14:36:24 +01001313Using `:const` for constants is common, but the semantics varies. Some
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001314languages only make the variable immutable, others also make the value
1315immutable. Since "final" is well known from Java for only making the variable
1316immutable we decided to use that. And then `:const` can be used for making
1317both immutable. This was also used in legacy Vim script and the meaning is
1318almost the same.
1319
1320What we end up with is very similar to Dart: >
1321 :var name # mutable variable and value
1322 :final name # immutable variable, mutable value
1323 :const name # immutable variable and value
1324
1325Since legacy and Vim9 script will be mixed and global variables will be
1326shared, optional type checking is desirable. Also, type inference will avoid
1327the need for specifying the type in many cases. The TypeScript syntax fits
1328best for adding types to declarations: >
1329 var name: string # string type is specified
1330 ...
1331 name = 'John'
1332 const greeting = 'hello' # string type is inferred
1333
1334This is how we put types in a declaration: >
1335 var mylist: list<string>
1336 final mylist: list<string> = ['foo']
1337 def Func(arg1: number, arg2: string): bool
1338
1339Two alternatives were considered:
13401. Put the type before the name, like Dart: >
1341 var list<string> mylist
1342 final list<string> mylist = ['foo']
1343 def Func(number arg1, string arg2) bool
13442. Put the type after the variable name, but do not use a colon, like Go: >
1345 var mylist list<string>
1346 final mylist list<string> = ['foo']
1347 def Func(arg1 number, arg2 string) bool
1348
1349The first is more familiar for anyone used to C or Java. The second one
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001350doesn't really have an advantage over the first, so let's discard the second.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001351
1352Since we use type inference the type can be left out when it can be inferred
1353from the value. This means that after `var` we don't know if a type or a name
1354follows. That makes parsing harder, not only for Vim but also for humans.
1355Also, it will not be allowed to use a variable name that could be a type name,
1356using `var string string` is too confusing.
1357
1358The chosen syntax, using a colon to separate the name from the type, adds
1359punctuation, but it actually makes it easier to recognize the parts of a
1360declaration.
1361
1362
1363Expressions ~
1364
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001365Expression evaluation was already close to what other languages are doing.
1366Some details are unexpected and can be improved. For example a boolean
1367condition would accept a string, convert it to a number and check if the
1368number is non-zero. This is unexpected and often leads to mistakes, since
1369text not starting with a number would be converted to zero, which is
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001370considered false. Thus using a string for a condition would often not give an
1371error and be considered false. That is confusing.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001372
Bram Moolenaar23515b42020-11-29 14:36:24 +01001373In Vim9 type checking is stricter to avoid mistakes. Where a condition is
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001374used, e.g. with the `:if` command and the `||` operator, only boolean-like
1375values are accepted:
1376 true: `true`, `v:true`, `1`, `0 < 9`
1377 false: `false`, `v:false`, `0`, `0 > 9`
1378Note that the number zero is false and the number one is true. This is more
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001379permissive than most other languages. It was done because many builtin
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001380functions return these values.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001381
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001382If you have any type of value and want to use it as a boolean, use the `!!`
1383operator:
1384 true: !`!'text'`, `!![99]`, `!!{'x': 1}`, `!!99`
1385 false: `!!''`, `!![]`, `!!{}`
1386
1387From a language like JavaScript we have this handy construct: >
1388 GetName() || 'unknown'
1389However, this conflicts with only allowing a boolean for a condition.
1390Therefore the "??" operator was added: >
1391 GetName() ?? 'unknown'
1392Here you can explicitly express your intention to use the value as-is and not
1393result in a boolean. This is called the |falsy-operator|.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001394
1395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396Import and Export ~
1397
1398A problem of legacy Vim script is that by default all functions and variables
1399are global. It is possible to make them script-local, but then they are not
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001400available in other scripts. This defies the concept of a package that only
1401exports selected items and keeps the rest local.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402
Bram Moolenaar3d1cde82020-08-15 18:55:18 +02001403In Vim9 script a mechanism very similar to the JavaScript import and export
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404mechanism is supported. It is a variant to the existing `:source` command
1405that works like one would expect:
1406- Instead of making everything global by default, everything is script-local,
1407 unless exported.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001408- When importing a script the symbols that are imported are explicitly listed,
1409 avoiding name conflicts and failures if functionality is added later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410- The mechanism allows for writing a big, long script with a very clear API:
1411 the exported function(s) and class(es).
1412- By using relative paths loading can be much faster for an import inside of a
1413 package, no need to search many directories.
1414- Once an import has been used, it can be cached and loading it again can be
1415 avoided.
1416- The Vim-specific use of "s:" to make things script-local can be dropped.
1417
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001418When sourcing a Vim9 script from a legacy script, only the items defined
1419globally can be used, not the exported items. Alternatives considered:
1420- All the exported items become available as script-local items. This makes
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001421 it uncontrollable what items get defined and likely soon leads to trouble.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001422- Use the exported items and make them global. Disadvantage is that it's then
1423 not possible to avoid name clashes in the global namespace.
1424- Completely disallow sourcing a Vim9 script, require using `:import`. That
1425 makes it difficult to use scripts for testing, or sourcing them from the
1426 command line to try them out.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001427Note that you can also use `:import` in legacy Vim script, see above.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001430Compiling functions early ~
1431
1432Functions are compiled when called or when `:defcompile` is used. Why not
1433compile them early, so that syntax and type errors are reported early?
1434
1435The functions can't be compiled right away when encountered, because there may
1436be forward references to functions defined later. Consider defining functions
1437A, B and C, where A calls B, B calls C, and C calls A again. It's impossible
1438to reorder the functions to avoid forward references.
1439
1440An alternative would be to first scan through the file to locate items and
1441figure out their type, so that forward references are found, and only then
1442execute the script and compile the functions. This means the script has to be
1443parsed twice, which is slower, and some conditions at the script level, such
1444as checking if a feature is supported, are hard to use. An attempt was made
1445to see if it works, but it turned out to be impossible to make work nicely.
1446
1447It would be possible to compile all the functions at the end of the script.
1448The drawback is that if a function never gets called, the overhead of
1449compiling it counts anyway. Since startup speed is very important, in most
1450cases it's better to do it later and accept that syntax and type errors are
1451only reported then. In case these errors should be found early, e.g. when
1452testing, the `:defcompile` command will help out.
1453
1454
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001455Why not use an embedded language? ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456
1457Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001458these interfaces have never become widely used, for various reasons. When
1459Vim9 was designed a decision was made to make these interfaces lower priority
1460and concentrate on Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001462Still, plugin writers may find other languages more familiar, want to use
1463existing libraries or see a performance benefit. We encourage plugin authors
1464to write code in any language and run it as an external tool, using jobs and
1465channels. We can try to make this easier somehow.
1466
1467Using an external tool also has disadvantages. An alternative is to convert
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468the tool into Vim script. For that to be possible without too much
1469translation, and keeping the code fast at the same time, the constructs of the
1470tool need to be supported. Since most languages support classes the lack of
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001471support for classes in Vim is then a problem.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001473
1474Classes ~
1475
1476Vim supports a kind-of object oriented programming by adding methods to a
1477dictionary. With some care this can be made to work, but it does not look
1478like real classes. On top of that, it's quite slow, because of the use of
1479dictionaries.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480
1481The support of classes in Vim9 script is a "minimal common functionality" of
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001482class support in most languages. It works much like Java, which is the most
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483popular programming language.
1484
1485
1486
1487 vim:tw=78:ts=8:noet:ft=help:norl: