blob: 93c3ddd2f93c7bb0d4381e4ef4aa7edeb9678f76 [file] [log] [blame]
Bram Moolenaar98a29d02021-01-18 19:55:44 +01001*vim9.txt* For Vim version 8.2. Last change: 2021 Jan 15
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 Moolenaar2b327002020-12-26 15:39:31 +0100406To avoid the "{" of a dictionary literal to be recognized as a statement block
407wrap it in parenthesis: >
408 var Lambda = (arg) => ({key: 42})
Bram Moolenaar65c44152020-12-24 15:14:01 +0100409
410
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200411Automatic line continuation ~
412
413In many cases it is obvious that an expression continues on the next line. In
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100414those cases there is no need to prefix the line with a backslash (see
415|line-continuation|). For example, when a list spans multiple lines: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200416 var mylist = [
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200417 'one',
418 'two',
419 ]
Bram Moolenaare6085c52020-04-12 20:19:16 +0200420And when a dict spans multiple lines: >
Bram Moolenaar2bede172020-11-19 18:53:18 +0100421 var mydict = {
Bram Moolenaare6085c52020-04-12 20:19:16 +0200422 one: 1,
423 two: 2,
424 }
425Function call: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200426 var result = Func(
Bram Moolenaare6085c52020-04-12 20:19:16 +0200427 arg1,
428 arg2
429 )
430
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200431For binary operators in expressions not in [], {} or () a line break is
432possible just before or after the operator. For example: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200433 var text = lead
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200434 .. middle
435 .. end
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200436 var total = start +
Bram Moolenaar82be4842021-01-11 19:40:15 +0100437 end -
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200438 correction
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200439 var result = positive
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200440 ? PosFunc(arg)
441 : NegFunc(arg)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200442
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200443For a method call using "->" and a member using a dot, a line break is allowed
444before it: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200445 var result = GetBuilder()
Bram Moolenaar73fef332020-06-21 22:12:03 +0200446 ->BuilderSetWidth(333)
447 ->BuilderSetHeight(777)
448 ->BuilderBuild()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200449 var result = MyDict
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200450 .member
Bram Moolenaar73fef332020-06-21 22:12:03 +0200451
Bram Moolenaardcc58e02020-12-28 20:53:21 +0100452For commands that have an argument that is a list of commands, the | character
453at the start of the line indicates line continuation: >
454 autocmd BufNewFile *.match if condition
455 | echo 'match'
456 | endif
457
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200458< *E1050*
459To make it possible for the operator at the start of the line to be
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200460recognized, it is required to put a colon before a range. This will add
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200461"start" and print: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200462 var result = start
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200463 + print
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200464Like this: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200465 var result = start + print
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200466
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200467This will assign "start" and print a line: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200468 var result = start
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200469 :+ print
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200470
Bram Moolenaar23515b42020-11-29 14:36:24 +0100471Note that the colon is not required for the |+cmd| argument: >
472 edit +6 fname
473
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200474It is also possible to split a function header over multiple lines, in between
475arguments: >
476 def MyFunc(
477 text: string,
478 separator = '-'
479 ): string
480
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100481Since a continuation line cannot be easily recognized the parsing of commands
Bram Moolenaar65c44152020-12-24 15:14:01 +0100482has been made stricter. E.g., because of the error in the first line, the
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100483second line is seen as a separate command: >
484 popup_create(some invalid expression, {
485 exit_cb: Func})
486Now "exit_cb: Func})" is actually a valid command: save any changes to the
487file "_cb: Func})" and exit. To avoid this kind of mistake in Vim9 script
488there must be white space between most command names and the argument.
489
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100490However, the argument of a command that is a command won't be recognized. For
491example, after "windo echo expr" a line break inside "expr" will not be seen.
492
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100493
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200494Notes:
495- "enddef" cannot be used at the start of a continuation line, it ends the
496 current function.
497- No line break is allowed in the LHS of an assignment. Specifically when
498 unpacking a list |:let-unpack|. This is OK: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200499 [var1, var2] =
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200500 Func()
501< This does not work: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200502 [var1,
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200503 var2] =
504 Func()
505- No line break is allowed in between arguments of an `:echo`, `:execute` and
506 similar commands. This is OK: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200507 echo [1,
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200508 2] [3,
509 4]
510< This does not work: >
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200511 echo [1, 2]
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200512 [3, 4]
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514No curly braces expansion ~
515
516|curly-braces-names| cannot be used.
517
518
Bram Moolenaar2bede172020-11-19 18:53:18 +0100519Dictionary literals ~
520
521Traditionally Vim has supported dictionary literals with a {} syntax: >
522 let dict = {'key': value}
523
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100524Later it became clear that using a simple text key is very common, thus
525literal dictionaries were introduced in a backwards compatible way: >
Bram Moolenaar2bede172020-11-19 18:53:18 +0100526 let dict = #{key: value}
527
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100528However, this #{} syntax is unlike any existing language. As it turns out
529that using a literal key is much more common than using an expression, and
Bram Moolenaar2bede172020-11-19 18:53:18 +0100530considering that JavaScript uses this syntax, using the {} form for dictionary
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100531literals is considered a much more useful syntax. In Vim9 script the {} form
Bram Moolenaar2bede172020-11-19 18:53:18 +0100532uses literal keys: >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100533 var dict = {key: value}
Bram Moolenaar2bede172020-11-19 18:53:18 +0100534
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100535This works for alphanumeric characters, underscore and dash. If you want to
536use another character, use a single or double quoted string: >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100537 var dict = {'key with space': value}
538 var dict = {"key\twith\ttabs": value}
539 var dict = {'': value} # empty key
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100540
541In case the key needs to be an expression, square brackets can be used, just
542like in JavaScript: >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100543 var dict = {["key" .. nr]: value}
Bram Moolenaar2bede172020-11-19 18:53:18 +0100544
545
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200546No :xit, :t, :append, :change or :insert ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100547
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200548These commands are too easily confused with local variable names.
549Instead of `:x` or `:xit` you can use `:exit`.
550Instead of `:t` you can use `:copy`.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100551
552
553Comparators ~
554
555The 'ignorecase' option is not used for comparators that use strings.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556
557
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100558For loop ~
559
560Legacy Vim script has some tricks to make a for loop over a list handle
561deleting items at the current or previous item. In Vim9 script it just uses
562the index, if items are deleted then items in the list will be skipped.
563Example legacy script: >
564 let l = [1, 2, 3, 4]
565 for i in l
566 echo i
567 call remove(l, index(l, i))
568 endfor
569Would echo:
570 1
571 2
572 3
573 4
574In compiled Vim9 script you get:
575 1
576 3
577Generally, you should not change the list that is iterated over. Make a copy
578first if needed.
579
580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581White space ~
582
583Vim9 script enforces proper use of white space. This is no longer allowed: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200584 var name=234 # Error!
585 var name= 234 # Error!
586 var name =234 # Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587There must be white space before and after the "=": >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200588 var name = 234 # OK
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200589White space must also be put before the # that starts a comment after a
590command: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200591 var name = 234# Error!
592 var name = 234 # OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593
594White space is required around most operators.
595
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100596White space is required in a sublist (list slice) around the ":", except at
597the start and end: >
598 otherlist = mylist[v : count] # v:count has a different meaning
599 otherlist = mylist[:] # make a copy of the List
600 otherlist = mylist[v :]
601 otherlist = mylist[: v]
602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603White space is not allowed:
604- Between a function name and the "(": >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100605 Func (arg) # Error!
606 Func
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200607 \ (arg) # Error!
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100608 Func
609 (arg) # Error!
610 Func(arg) # OK
611 Func(
612 arg) # OK
613 Func(
614 arg # OK
615 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616
617
618Conditions and expressions ~
619
Bram Moolenaar13106602020-10-04 16:06:05 +0200620Conditions and expressions are mostly working like they do in other languages.
621Some values are different from legacy Vim script:
622 value legacy Vim script Vim9 script ~
623 0 falsy falsy
624 1 truthy truthy
625 99 truthy Error!
626 "0" falsy Error!
627 "99" truthy Error!
628 "text" falsy Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629
Bram Moolenaar13106602020-10-04 16:06:05 +0200630For the "??" operator and when using "!" then there is no error, every value
631is either falsy or truthy. This is mostly like JavaScript, except that an
632empty list and dict is falsy:
633
634 type truthy when ~
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100635 bool true, v:true or 1
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 number non-zero
637 float non-zero
638 string non-empty
639 blob non-empty
640 list non-empty (different from JavaScript)
641 dictionary non-empty (different from JavaScript)
Bram Moolenaard1caa942020-04-10 22:10:56 +0200642 func when there is a function name
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100643 special true or v:true
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 job when not NULL
645 channel when not NULL
646 class when not NULL
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100647 object when not NULL (TODO: when isTrue() returns true)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200649The boolean operators "||" and "&&" expect the values to be boolean, zero or
650one: >
651 1 || false == true
652 0 || 1 == true
653 0 || false == false
654 1 && true == true
655 0 && 1 == false
656 8 || 0 Error!
657 'yes' && 0 Error!
658 [] || 99 Error!
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200660When using "!" for inverting, there is no error for using any type and the
Bram Moolenaar13106602020-10-04 16:06:05 +0200661result is a boolean. "!!" can be used to turn any value into boolean: >
Bram Moolenaar82be4842021-01-11 19:40:15 +0100662 !'yes' == false
Bram Moolenaar13106602020-10-04 16:06:05 +0200663 !![] == false
Bram Moolenaar82be4842021-01-11 19:40:15 +0100664 !![1, 2, 3] == true
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200665
666When using "`.."` for string concatenation arguments of simple types are
Bram Moolenaar13106602020-10-04 16:06:05 +0200667always converted to string: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 'hello ' .. 123 == 'hello 123'
Bram Moolenaar7e6a5152021-01-02 16:39:53 +0100669 'hello ' .. v:true == 'hello true'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200671Simple types are string, float, special and bool. For other types |string()|
672can be used.
Bram Moolenaar67977822021-01-03 21:53:53 +0100673 *false* *true* *null*
674In Vim9 script one can use "true" for v:true, "false" for v:false and "null"
675for v:null. When converting a boolean to a string "false" and "true" are
676used, not "v:false" and "v:true" like in legacy script. "v:none" is not
677changed, it is only used in JSON and has no equivalent in other languages.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100679Indexing a string with [idx] or [idx : idx] uses character indexes instead of
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200680byte indexes. Example: >
681 echo 'bár'[1]
682In legacy script this results in the character 0xc3 (an illegal byte), in Vim9
683script this results in the string 'á'.
Bram Moolenaar82be4842021-01-11 19:40:15 +0100684A negative index is counting from the end, "[-1]" is the last character.
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100685To exclude the last character use |slice()|.
Bram Moolenaar82be4842021-01-11 19:40:15 +0100686If the index is out of range then an empty string results.
687
688In legacy script "++var" and "--var" would be silently accepted and have no
689effect. This is an error in Vim9 script.
690
691Numbers starting with zero are not considered to be octal, only numbers
692starting with "0o" are octal: "0o744". |scriptversion-4|
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694
Bram Moolenaare46a4402020-06-30 20:38:27 +0200695What to watch out for ~
696 *vim9-gotchas*
697Vim9 was designed to be closer to often used programming languages, but at the
698same time tries to support the legacy Vim commands. Some compromises had to
699be made. Here is a summary of what might be unexpected.
700
701Ex command ranges need to be prefixed with a colon. >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100702 -> legacy Vim: shifts the previous line to the right
703 ->func() Vim9: method call in a continuation line
704 :-> Vim9: shifts the previous line to the right
Bram Moolenaare46a4402020-06-30 20:38:27 +0200705
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100706 %s/a/b legacy Vim: substitute on all lines
Bram Moolenaare46a4402020-06-30 20:38:27 +0200707 x = alongname
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100708 % another Vim9: modulo operator in a continuation line
709 :%s/a/b Vim9: substitute on all lines
710 't legacy Vim: jump to mark t
711 'text'->func() Vim9: method call
712 :'t Vim9: jump to mark t
Bram Moolenaare46a4402020-06-30 20:38:27 +0200713
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200714Some Ex commands can be confused with assignments in Vim9 script: >
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100715 g:name = value # assignment
716 g:pattern:cmd # invalid command - ERROR
717 :g:pattern:cmd # :global command
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200718
Bram Moolenaare46a4402020-06-30 20:38:27 +0200719Functions defined with `:def` compile the whole function. Legacy functions
720can bail out, and the following lines are not parsed: >
721 func Maybe()
722 if !has('feature')
723 return
724 endif
725 use-feature
726 endfunc
727Vim9 functions are compiled as a whole: >
728 def Maybe()
729 if !has('feature')
730 return
731 endif
Bram Moolenaar82be4842021-01-11 19:40:15 +0100732 use-feature # May give a compilation error
Bram Moolenaare46a4402020-06-30 20:38:27 +0200733 enddef
734For a workaround, split it in two functions: >
735 func Maybe()
736 if has('feature')
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100737 call MaybeInner()
Bram Moolenaare46a4402020-06-30 20:38:27 +0200738 endif
739 endfunc
740 if has('feature')
741 def MaybeInner()
742 use-feature
743 enddef
744 endif
Bram Moolenaar1c6737b2020-09-07 22:18:52 +0200745Or put the unsupported code inside an `if` with a constant expression that
Bram Moolenaar207f0092020-08-30 17:20:20 +0200746evaluates to false: >
747 def Maybe()
748 if has('feature')
749 use-feature
750 endif
751 enddef
Bram Moolenaar82be4842021-01-11 19:40:15 +0100752< *vim9-user-command*
Bram Moolenaar98a29d02021-01-18 19:55:44 +0100753Another side effect of compiling a function is that the presence of a user
Bram Moolenaar82be4842021-01-11 19:40:15 +0100754command is checked at compile time. If the user command is defined later an
755error will result. This works: >
756 command -nargs=1 MyCommand echom <q-args>
757 def Works()
758 MyCommand 123
759 enddef
760This will give an error for "MyCommand" not being defined: >
761 def Works()
762 command -nargs=1 MyCommand echom <q-args>
763 MyCommand 123
764 enddef
765A workaround is to invoke the command indirectly with `:execute`: >
766 def Works()
767 command -nargs=1 MyCommand echom <q-args>
768 execute 'MyCommand 123'
769 enddef
770
Bram Moolenaar207f0092020-08-30 17:20:20 +0200771Note that for unrecognized commands there is no check for "|" and a following
772command. This will give an error for missing `endif`: >
773 def Maybe()
774 if has('feature') | use-feature | endif
775 enddef
Bram Moolenaare46a4402020-06-30 20:38:27 +0200776
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100777Other differences ~
778
779Patterns are used like 'magic' is set, unless explicitly overruled.
780The 'edcompatible' option value is not used.
781The 'gdefault' option value is not used.
782
783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784==============================================================================
785
7863. New style functions *fast-functions*
787
788THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
789
790 *:def*
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200791:def[!] {name}([arguments])[: {return-type}]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 Define a new function by the name {name}. The body of
793 the function follows in the next lines, until the
794 matching `:enddef`.
795
Bram Moolenaard77a8522020-04-03 21:59:57 +0200796 When {return-type} is omitted or is "void" the
797 function is not expected to return anything.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798
799 {arguments} is a sequence of zero or more argument
800 declarations. There are three forms:
801 {name}: {type}
802 {name} = {value}
803 {name}: {type} = {value}
804 The first form is a mandatory argument, the caller
805 must always provide them.
806 The second and third form are optional arguments.
807 When the caller omits an argument the {value} is used.
808
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200809 The function will be compiled into instructions when
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200810 called, or when `:disassemble` or `:defcompile` is
811 used. Syntax and type errors will be produced at that
812 time.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200813
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200814 It is possible to nest `:def` inside another `:def` or
815 `:function` up to about 50 levels deep.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100816
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200817 [!] is used as with `:function`. Note that
818 script-local functions cannot be deleted or redefined
819 later in Vim9 script. They can only be removed by
820 reloading the same script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821
822 *:enddef*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200823:enddef End of a function defined with `:def`. It should be on
824 a line by its own.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825
826
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100827If the script the function is defined in is Vim9 script, then script-local
828variables can be accessed without the "s:" prefix. They must be defined
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200829before the function is compiled. If the script the function is defined in is
830legacy script, then script-local variables must be accessed with the "s:"
Bram Moolenaar207f0092020-08-30 17:20:20 +0200831prefix and they do not need to exist (they can be deleted any time).
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100832
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200833 *:defc* *:defcompile*
834:defc[ompile] Compile functions defined in the current script that
835 were not compiled yet.
836 This will report errors found during the compilation.
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100837
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100838 *:disa* *:disassemble*
839:disa[ssemble] {func} Show the instructions generated for {func}.
840 This is for debugging and testing.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100841 Note that for command line completion of {func} you
842 can prepend "s:" to find script-local functions.
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100843
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200844Limitations ~
845
846Local variables will not be visible to string evaluation. For example: >
Bram Moolenaar2b327002020-12-26 15:39:31 +0100847 def MapList(): list<string>
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200848 var list = ['aa', 'bb', 'cc', 'dd']
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200849 return range(1, 2)->map('list[v:val]')
850 enddef
851
852The map argument is a string expression, which is evaluated without the
853function scope. Instead, use a lambda: >
Bram Moolenaar2b327002020-12-26 15:39:31 +0100854 def MapList(): list<string>
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200855 var list = ['aa', 'bb', 'cc', 'dd']
Bram Moolenaar2b327002020-12-26 15:39:31 +0100856 return range(1, 2)->map(( _, v) => list[v])
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200857 enddef
858
Bram Moolenaar2b327002020-12-26 15:39:31 +0100859The same is true for commands that are not compiled, such as `:global`.
860For these the backtick expansion can be used. Example: >
861 def Replace()
862 var newText = 'blah'
863 g/pattern/s/^/`=newText`/
864 enddef
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866==============================================================================
867
8684. Types *vim9-types*
869
870THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
871
872The following builtin types are supported:
873 bool
874 number
875 float
876 string
877 blob
Bram Moolenaard77a8522020-04-03 21:59:57 +0200878 list<{type}>
879 dict<{type}>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 job
881 channel
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100882 func
Bram Moolenaard1caa942020-04-10 22:10:56 +0200883 func: {type}
Bram Moolenaard77a8522020-04-03 21:59:57 +0200884 func({type}, ...)
885 func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886
887Not supported yet:
Bram Moolenaard77a8522020-04-03 21:59:57 +0200888 tuple<a: {type}, b: {type}, ...>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
Bram Moolenaard77a8522020-04-03 21:59:57 +0200890These types can be used in declarations, but no value will have this type:
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200891 {type}|{type} {not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 void
893 any
894
Bram Moolenaard77a8522020-04-03 21:59:57 +0200895There is no array type, use list<{type}> instead. For a list constant an
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896efficient implementation is used that avoids allocating lot of small pieces of
897memory.
898
Bram Moolenaard77a8522020-04-03 21:59:57 +0200899A partial and function can be declared in more or less specific ways:
900func any kind of function reference, no type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200901 checking for arguments or return value
Bram Moolenaard77a8522020-04-03 21:59:57 +0200902func: {type} any number and type of arguments with specific
903 return type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200904func({type}) function with argument type, does not return
Bram Moolenaard77a8522020-04-03 21:59:57 +0200905 a value
Bram Moolenaard1caa942020-04-10 22:10:56 +0200906func({type}): {type} function with argument type and return type
907func(?{type}) function with type of optional argument, does
908 not return a value
909func(...{type}) function with type of variable number of
910 arguments, does not return a value
911func({type}, ?{type}, ...{type}): {type}
912 function with:
913 - type of mandatory argument
914 - type of optional argument
915 - type of variable number of arguments
916 - return type
Bram Moolenaard77a8522020-04-03 21:59:57 +0200917
918If the return type is "void" the function does not return a value.
919
920The reference can also be a |Partial|, in which case it stores extra arguments
921and/or a dictionary, which are not visible to the caller. Since they are
922called in the same way the declaration is the same.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923
924Custom types can be defined with `:type`: >
925 :type MyList list<string>
Bram Moolenaar127542b2020-08-09 17:22:04 +0200926Custom types must start with a capital letter, to avoid name clashes with
927builtin types added later, similarly to user functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928{not implemented yet}
929
930And classes and interfaces can be used as types: >
931 :class MyClass
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200932 :var mine: MyClass
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
934 :interface MyInterface
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200935 :var mine: MyInterface
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936
937 :class MyTemplate<Targ>
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200938 :var mine: MyTemplate<number>
939 :var mine: MyTemplate<string>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940
941 :class MyInterface<Targ>
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200942 :var mine: MyInterface<number>
943 :var mine: MyInterface<string>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944{not implemented yet}
945
946
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200947Variable types and type casting ~
948 *variable-types*
Bram Moolenaar64d662d2020-08-09 19:02:50 +0200949Variables declared in Vim9 script or in a `:def` function have a type, either
950specified explicitly or inferred from the initialization.
951
952Global, buffer, window and tab page variables do not have a specific type, the
953value can be changed at any time, possibly changing the type. Therefore, in
954compiled code the "any" type is assumed.
955
956This can be a problem when the "any" type is undesired and the actual type is
957expected to always be the same. For example, when declaring a list: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200958 var l: list<number> = [1, g:two]
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100959At compile time Vim doesn't know the type of "g:two" and the expression type
960becomes list<any>. An instruction is generated to check the list type before
961doing the assignment, which is a bit inefficient.
962 *type-casting*
963To avoid this, use a type cast: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200964 var l: list<number> = [1, <number>g:two]
Bram Moolenaar4072ba52020-12-23 13:56:35 +0100965The compiled code will then only check that "g:two" is a number and give an
966error if it isn't. This is called type casting.
Bram Moolenaar64d662d2020-08-09 19:02:50 +0200967
968The syntax of a type cast is: "<" {type} ">". There cannot be white space
969after the "<" or before the ">" (to avoid them being confused with
970smaller-than and bigger-than operators).
971
972The semantics is that, if needed, a runtime type check is performed. The
973value is not actually changed. If you need to change the type, e.g. to change
974it to a string, use the |string()| function. Or use |str2nr()| to convert a
975string to a number.
976
977
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200978Type inference ~
979 *type-inference*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980In general: Whenever the type is clear it can be omitted. For example, when
981declaring a variable and giving it a value: >
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200982 var name = 0 # infers number type
983 var name = 'hello' # infers string type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984
Bram Moolenaar127542b2020-08-09 17:22:04 +0200985The type of a list and dictionary comes from the common type of the values.
986If the values all have the same type, that type is used for the list or
987dictionary. If there is a mix of types, the "any" type is used. >
988 [1, 2, 3] list<number>
989 ['a', 'b', 'c'] list<string>
990 [1, 'x', 3] list<any>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991
Bram Moolenaar207f0092020-08-30 17:20:20 +0200992
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200993Stricter type checking ~
994 *type-checking*
Bram Moolenaar207f0092020-08-30 17:20:20 +0200995In legacy Vim script, where a number was expected, a string would be
996automatically converted to a number. This was convenient for an actual number
997such as "123", but leads to unexpected problems (but no error message) if the
998string doesn't start with a number. Quite often this leads to hard-to-find
999bugs.
1000
1001In Vim9 script this has been made stricter. In most places it works just as
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001002before, if the value used matches the expected type. There will sometimes be
1003an error, thus breaking backwards compatibility. For example:
Bram Moolenaar207f0092020-08-30 17:20:20 +02001004- Using a number other than 0 or 1 where a boolean is expected. *E1023*
1005- Using a string value when setting a number options.
1006- Using a number where a string is expected. *E1024*
1007
Bram Moolenaar82be4842021-01-11 19:40:15 +01001008One consequence is that the item type of a list or dict given to map() must
1009not change. This will give an error in compiled code: >
1010 map([1, 2, 3], (i, v) => 'item ' .. i)
1011 E1012: Type mismatch; expected list<number> but got list<string>
1012Instead use |mapnew()|.
1013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014==============================================================================
1015
Bram Moolenaar30fd8202020-09-26 15:09:30 +020010165. Namespace, Import and Export
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 *vim9script* *vim9-export* *vim9-import*
1018
1019THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
1020
1021A Vim9 script can be written to be imported. This means that everything in
1022the script is local, unless exported. Those exported items, and only those
1023items, can then be imported in another script.
1024
Bram Moolenaar207f0092020-08-30 17:20:20 +02001025You can cheat by using the global namespace explicitly. We will assume here
1026that you don't do that.
1027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028
1029Namespace ~
Bram Moolenaardcc58e02020-12-28 20:53:21 +01001030 *vim9-namespace*
Bram Moolenaar560979e2020-02-04 22:53:05 +01001031To recognize a file that can be imported the `vim9script` statement must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032appear as the first statement in the file. It tells Vim to interpret the
1033script in its own namespace, instead of the global namespace. If a file
1034starts with: >
1035 vim9script
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001036 var myvar = 'yes'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037Then "myvar" will only exist in this file. While without `vim9script` it would
1038be available as `g:myvar` from any other script and function.
1039
1040The variables at the file level are very much like the script-local "s:"
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +02001041variables in legacy Vim script, but the "s:" is omitted. And they cannot be
1042deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +02001044In Vim9 script the global "g:" namespace can still be used as before. And the
1045"w:", "b:" and "t:" namespaces. These have in common that variables are not
1046declared and they can be deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047
1048A side effect of `:vim9script` is that the 'cpoptions' option is set to the
1049Vim default value, like with: >
1050 :set cpo&vim
1051One of the effects is that |line-continuation| is always enabled.
1052The original value of 'cpoptions' is restored at the end of the script.
1053
1054
1055Export ~
1056 *:export* *:exp*
Bram Moolenaar2547aa92020-07-26 17:00:44 +02001057Exporting an item can be written as: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 export const EXPORTED_CONST = 1234
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001059 export var someValue = ...
1060 export final someValue = ...
1061 export const someValue = ...
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062 export def MyFunc() ...
1063 export class MyClass ...
1064
1065As this suggests, only constants, variables, `:def` functions and classes can
Bram Moolenaar2547aa92020-07-26 17:00:44 +02001066be exported. {classes are not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001068 *E1042*
1069`:export` can only be used in Vim9 script, at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070
1071
1072Import ~
Bram Moolenaar73fef332020-06-21 22:12:03 +02001073 *:import* *:imp* *E1094*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074The exported items can be imported individually in another Vim9 script: >
1075 import EXPORTED_CONST from "thatscript.vim"
1076 import MyClass from "myclass.vim"
1077
1078To import multiple items at the same time: >
1079 import {someValue, MyClass} from "thatscript.vim"
1080
Bram Moolenaar560979e2020-02-04 22:53:05 +01001081In case the name is ambiguous, another name can be specified: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 import MyClass as ThatClass from "myclass.vim"
1083 import {someValue, MyClass as ThatClass} from "myclass.vim"
1084
1085To import all exported items under a specific identifier: >
1086 import * as That from 'thatscript.vim'
1087
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001088{not implemented yet: using "This as That"}
1089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free
1091to choose the name "That", but it is highly recommended to use the name of the
1092script file to avoid confusion.
1093
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001094`:import` can also be used in legacy Vim script. The imported items still
1095become script-local, even when the "s:" prefix is not given.
1096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097The script name after `import` can be:
1098- A relative path, starting "." or "..". This finds a file relative to the
1099 location of the script file itself. This is useful to split up a large
1100 plugin into several files.
1101- An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001102 will rarely be used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103- A path not being relative or absolute. This will be found in the
1104 "import" subdirectories of 'runtimepath' entries. The name will usually be
1105 longer and unique, to avoid loading the wrong file.
1106
1107Once a vim9 script file has been imported, the result is cached and used the
1108next time the same script is imported. It will not be read again.
1109 *:import-cycle*
1110The `import` commands are executed when encountered. If that script (directly
1111or indirectly) imports the current script, then items defined after the
1112`import` won't be processed yet. Therefore cyclic imports can exist, but may
1113result in undefined items.
1114
1115
1116Import in an autoload script ~
1117
1118For optimal startup speed, loading scripts should be postponed until they are
Bram Moolenaar560979e2020-02-04 22:53:05 +01001119actually needed. A recommended mechanism:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120
11211. In the plugin define user commands, functions and/or mappings that refer to
1122 an autoload script. >
Bram Moolenaar98a29d02021-01-18 19:55:44 +01001123 command -nargs=1 SearchForStuff searchfor#Stuff(<f-args>)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124
1125< This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen.
1126
Bram Moolenaar3d1cde82020-08-15 18:55:18 +020011272. In the autoload script do the actual work. You can import items from
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 other files to split up functionality in appropriate pieces. >
1129 vim9script
Bram Moolenaar82be4842021-01-11 19:40:15 +01001130 import FilterFunc from "../import/someother.vim"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 def searchfor#Stuff(arg: string)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001132 var filtered = FilterFunc(arg)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 ...
1134< This goes in .../autoload/searchfor.vim. "searchfor" in the file name
1135 must be exactly the same as the prefix for the function name, that is how
1136 Vim finds the file.
1137
11383. Other functionality, possibly shared between plugins, contains the exported
1139 items and any private items. >
1140 vim9script
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001141 var localVar = 'local'
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001142 export def FilterFunc(arg: string): string
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 ...
1144< This goes in .../import/someother.vim.
1145
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001146When compiling a `:def` function and a function in an autoload script is
1147encountered, the script is not loaded until the `:def` function is called.
1148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149
1150Import in legacy Vim script ~
1151
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001152If an `import` statement is used in legacy Vim script, the script-local "s:"
1153namespace will be used for the imported item, even when "s:" is not specified.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154
1155
1156==============================================================================
1157
Bram Moolenaar1d59aa12020-09-19 18:50:13 +020011586. Future work: classes *vim9-classes*
1159
1160Above "class" was mentioned a few times, but it has not been implemented yet.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001161Most of Vim9 script can be created without this functionality, and since
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001162implementing classes is going to be a lot of work, it is left for the future.
1163For now we'll just make sure classes can be added later.
1164
1165Thoughts:
1166- `class` / `endclass`, everything in one file
1167- Class names are always CamelCase
1168- Single constructor
1169- Single inheritance with `class ThisClass extends BaseClass`
1170- `abstract class`
1171- `interface` (Abstract class without any implementation)
1172- `class SomeClass implements SomeInterface`
1173- Generics for class: `class <Tkey, Tentry>`
1174- Generics for function: `def <Tkey> GetLast(key: Tkey)`
1175
1176Again, much of this is from TypeScript.
1177
1178Some things that look like good additions:
1179- Use a class as an interface (like Dart)
1180- Extend a class with methods, using an import (like Dart)
1181
1182An important class that will be provided is "Promise". Since Vim is single
1183threaded, connecting asynchronous operations is a natural way of allowing
1184plugins to do their work without blocking the user. It's a uniform way to
1185invoke callbacks and handle timeouts and errors.
1186
1187==============================================================================
1188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010011899. Rationale *vim9-rationale*
1190
1191The :def command ~
1192
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001193Plugin writers have asked for much faster Vim script. Investigations have
Bram Moolenaar560979e2020-02-04 22:53:05 +01001194shown that keeping the existing semantics of function calls make this close to
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195impossible, because of the overhead involved with calling a function, setting
1196up the local function scope and executing lines. There are many details that
1197need to be handled, such as error messages and exceptions. The need to create
1198a dictionary for a: and l: scopes, the a:000 list and several others add too
1199much overhead that cannot be avoided.
1200
1201Therefore the `:def` method to define a new-style function had to be added,
1202which allows for a function with different semantics. Most things still work
1203as before, but some parts do not. A new way to define a function was
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001204considered the best way to separate the legacy style code from Vim9 style code.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205
1206Using "def" to define a function comes from Python. Other languages use
1207"function" which clashes with legacy Vim script.
1208
1209
1210Type checking ~
1211
1212When compiling lines of Vim commands into instructions as much as possible
1213should be done at compile time. Postponing it to runtime makes the execution
1214slower and means mistakes are found only later. For example, when
1215encountering the "+" character and compiling this into a generic add
Bram Moolenaar98a29d02021-01-18 19:55:44 +01001216instruction, at runtime the instruction would have to inspect the type of the
1217arguments and decide what kind of addition to do. And when the type is
1218dictionary throw an error. If the types are known to be numbers then an "add
1219number" instruction can be used, which is faster. The error can be given at
1220compile time, no error handling is needed at runtime, since adding two numbers
1221cannot fail.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001223The syntax for types, using <type> for compound types, is similar to Java. It
1224is easy to understand and widely used. The type names are what were used in
1225Vim before, with some additions such as "void" and "bool".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226
1227
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001228Removing clutter and weirdness ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001229
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001230Once decided that `:def` functions have different syntax than legacy functions,
1231we are free to add improvements to make the code more familiar for users who
1232know popular programming languages. In other words: remove weird things that
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001233only Vim does.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001234
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001235We can also remove clutter, mainly things that were done to make Vim script
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001236backwards compatible with the good old Vi commands.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001237
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001238Examples:
1239- Drop `:call` for calling a function and `:eval` for manipulating data.
1240- Drop using a leading backslash for line continuation, automatically figure
1241 out where an expression ends.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001242
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001243However, this does require that some things need to change:
1244- Comments start with # instead of ", to avoid confusing them with strings.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001245 This is good anyway, it is known from several popular languages.
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001246- Ex command ranges need to be prefixed with a colon, to avoid confusion with
1247 expressions (single quote can be a string or a mark, "/" can be divide or a
1248 search command, etc.).
1249
1250Goal is to limit the differences. A good criteria is that when the old syntax
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001251is accidentally used you are very likely to get an error message.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001252
1253
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001254Syntax and semantics from popular languages ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255
1256Script writers have complained that the Vim script syntax is unexpectedly
1257different from what they are used to. To reduce this complaint popular
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001258languages are used as an example. At the same time, we do not want to abandon
1259the well-known parts of legacy Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001261For many things TypeScript is followed. It's a recent language that is
1262gaining popularity and has similarities with Vim script. It also has a
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001263mix of static typing (a variable always has a known value type) and dynamic
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001264typing (a variable can have different types, this changes at runtime). Since
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001265legacy Vim script is dynamically typed and a lot of existing functionality
1266(esp. builtin functions) depends on that, while static typing allows for much
1267faster execution, we need to have this mix in Vim9 script.
1268
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001269There is no intention to completely match TypeScript syntax and semantics. We
1270just want to take those parts that we can use for Vim and we expect Vim users
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001271will be happy with. TypeScript is a complex language with its own history,
1272advantages and disadvantages. To get an idea of the disadvantages read the
1273book: "JavaScript: The Good Parts". Or find the article "TypeScript: the good
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001274parts" and read the "Things to avoid" section.
1275
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001276People familiar with other languages (Java, Python, etc.) will also find
1277things in TypeScript that they do not like or do not understand. We'll try to
1278avoid those things.
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001279
1280Specific items from TypeScript we avoid:
1281- Overloading "+", using it both for addition and string concatenation. This
1282 goes against legacy Vim script and often leads to mistakes. For that reason
1283 we will keep using ".." for string concatenation. Lua also uses ".." this
1284 way. And it allows for conversion to string for more values.
1285- TypeScript can use an expression like "99 || 'yes'" in a condition, but
1286 cannot assign the value to a boolean. That is inconsistent and can be
1287 annoying. Vim recognizes an expression with && or || and allows using the
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001288 result as a bool. TODO: to be reconsidered
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001289- TypeScript considers an empty string as Falsy, but an empty list or dict as
1290 Truthy. That is inconsistent. In Vim an empty list and dict are also
1291 Falsy.
1292- TypeScript has various "Readonly" types, which have limited usefulness,
1293 since a type cast can remove the immutable nature. Vim locks the value,
1294 which is more flexible, but is only checked at runtime.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001297Declarations ~
1298
1299Legacy Vim script uses `:let` for every assignment, while in Vim9 declarations
1300are used. That is different, thus it's good to use a different command:
1301`:var`. This is used in many languages. The semantics might be slightly
1302different, but it's easily recognized as a declaration.
1303
Bram Moolenaar23515b42020-11-29 14:36:24 +01001304Using `:const` for constants is common, but the semantics varies. Some
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001305languages only make the variable immutable, others also make the value
1306immutable. Since "final" is well known from Java for only making the variable
1307immutable we decided to use that. And then `:const` can be used for making
1308both immutable. This was also used in legacy Vim script and the meaning is
1309almost the same.
1310
1311What we end up with is very similar to Dart: >
1312 :var name # mutable variable and value
1313 :final name # immutable variable, mutable value
1314 :const name # immutable variable and value
1315
1316Since legacy and Vim9 script will be mixed and global variables will be
1317shared, optional type checking is desirable. Also, type inference will avoid
1318the need for specifying the type in many cases. The TypeScript syntax fits
1319best for adding types to declarations: >
1320 var name: string # string type is specified
1321 ...
1322 name = 'John'
1323 const greeting = 'hello' # string type is inferred
1324
1325This is how we put types in a declaration: >
1326 var mylist: list<string>
1327 final mylist: list<string> = ['foo']
1328 def Func(arg1: number, arg2: string): bool
1329
1330Two alternatives were considered:
13311. Put the type before the name, like Dart: >
1332 var list<string> mylist
1333 final list<string> mylist = ['foo']
1334 def Func(number arg1, string arg2) bool
13352. Put the type after the variable name, but do not use a colon, like Go: >
1336 var mylist list<string>
1337 final mylist list<string> = ['foo']
1338 def Func(arg1 number, arg2 string) bool
1339
1340The first is more familiar for anyone used to C or Java. The second one
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001341doesn't really have an advantage over the first, so let's discard the second.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001342
1343Since we use type inference the type can be left out when it can be inferred
1344from the value. This means that after `var` we don't know if a type or a name
1345follows. That makes parsing harder, not only for Vim but also for humans.
1346Also, it will not be allowed to use a variable name that could be a type name,
1347using `var string string` is too confusing.
1348
1349The chosen syntax, using a colon to separate the name from the type, adds
1350punctuation, but it actually makes it easier to recognize the parts of a
1351declaration.
1352
1353
1354Expressions ~
1355
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001356Expression evaluation was already close to what other languages are doing.
1357Some details are unexpected and can be improved. For example a boolean
1358condition would accept a string, convert it to a number and check if the
1359number is non-zero. This is unexpected and often leads to mistakes, since
1360text not starting with a number would be converted to zero, which is
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001361considered false. Thus using a string for a condition would often not give an
1362error and be considered false. That is confusing.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001363
Bram Moolenaar23515b42020-11-29 14:36:24 +01001364In Vim9 type checking is stricter to avoid mistakes. Where a condition is
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001365used, e.g. with the `:if` command and the `||` operator, only boolean-like
1366values are accepted:
1367 true: `true`, `v:true`, `1`, `0 < 9`
1368 false: `false`, `v:false`, `0`, `0 > 9`
1369Note that the number zero is false and the number one is true. This is more
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001370permissive than most other languages. It was done because many builtin
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001371functions return these values.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001372
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02001373If you have any type of value and want to use it as a boolean, use the `!!`
1374operator:
1375 true: !`!'text'`, `!![99]`, `!!{'x': 1}`, `!!99`
1376 false: `!!''`, `!![]`, `!!{}`
1377
1378From a language like JavaScript we have this handy construct: >
1379 GetName() || 'unknown'
1380However, this conflicts with only allowing a boolean for a condition.
1381Therefore the "??" operator was added: >
1382 GetName() ?? 'unknown'
1383Here you can explicitly express your intention to use the value as-is and not
1384result in a boolean. This is called the |falsy-operator|.
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001385
1386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387Import and Export ~
1388
1389A problem of legacy Vim script is that by default all functions and variables
1390are global. It is possible to make them script-local, but then they are not
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001391available in other scripts. This defies the concept of a package that only
1392exports selected items and keeps the rest local.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393
Bram Moolenaar3d1cde82020-08-15 18:55:18 +02001394In Vim9 script a mechanism very similar to the JavaScript import and export
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395mechanism is supported. It is a variant to the existing `:source` command
1396that works like one would expect:
1397- Instead of making everything global by default, everything is script-local,
1398 unless exported.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001399- When importing a script the symbols that are imported are explicitly listed,
1400 avoiding name conflicts and failures if functionality is added later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401- The mechanism allows for writing a big, long script with a very clear API:
1402 the exported function(s) and class(es).
1403- By using relative paths loading can be much faster for an import inside of a
1404 package, no need to search many directories.
1405- Once an import has been used, it can be cached and loading it again can be
1406 avoided.
1407- The Vim-specific use of "s:" to make things script-local can be dropped.
1408
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001409When sourcing a Vim9 script from a legacy script, only the items defined
1410globally can be used, not the exported items. Alternatives considered:
1411- All the exported items become available as script-local items. This makes
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001412 it uncontrollable what items get defined and likely soon leads to trouble.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001413- Use the exported items and make them global. Disadvantage is that it's then
1414 not possible to avoid name clashes in the global namespace.
1415- Completely disallow sourcing a Vim9 script, require using `:import`. That
1416 makes it difficult to use scripts for testing, or sourcing them from the
1417 command line to try them out.
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001418Note that you can also use `:import` in legacy Vim script, see above.
Bram Moolenaar65e0d772020-06-14 17:29:55 +02001419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001421Compiling functions early ~
1422
1423Functions are compiled when called or when `:defcompile` is used. Why not
1424compile them early, so that syntax and type errors are reported early?
1425
1426The functions can't be compiled right away when encountered, because there may
1427be forward references to functions defined later. Consider defining functions
1428A, B and C, where A calls B, B calls C, and C calls A again. It's impossible
1429to reorder the functions to avoid forward references.
1430
1431An alternative would be to first scan through the file to locate items and
1432figure out their type, so that forward references are found, and only then
1433execute the script and compile the functions. This means the script has to be
1434parsed twice, which is slower, and some conditions at the script level, such
1435as checking if a feature is supported, are hard to use. An attempt was made
1436to see if it works, but it turned out to be impossible to make work nicely.
1437
1438It would be possible to compile all the functions at the end of the script.
1439The drawback is that if a function never gets called, the overhead of
1440compiling it counts anyway. Since startup speed is very important, in most
1441cases it's better to do it later and accept that syntax and type errors are
1442only reported then. In case these errors should be found early, e.g. when
1443testing, the `:defcompile` command will help out.
1444
1445
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001446Why not use an embedded language? ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447
1448Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001449these interfaces have never become widely used, for various reasons. When
1450Vim9 was designed a decision was made to make these interfaces lower priority
1451and concentrate on Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001453Still, plugin writers may find other languages more familiar, want to use
1454existing libraries or see a performance benefit. We encourage plugin authors
1455to write code in any language and run it as an external tool, using jobs and
1456channels. We can try to make this easier somehow.
1457
1458Using an external tool also has disadvantages. An alternative is to convert
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459the tool into Vim script. For that to be possible without too much
1460translation, and keeping the code fast at the same time, the constructs of the
1461tool need to be supported. Since most languages support classes the lack of
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001462support for classes in Vim is then a problem.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463
Bram Moolenaar1d59aa12020-09-19 18:50:13 +02001464
1465Classes ~
1466
1467Vim supports a kind-of object oriented programming by adding methods to a
1468dictionary. With some care this can be made to work, but it does not look
1469like real classes. On top of that, it's quite slow, because of the use of
1470dictionaries.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471
1472The support of classes in Vim9 script is a "minimal common functionality" of
Bram Moolenaar1c6737b2020-09-07 22:18:52 +02001473class support in most languages. It works much like Java, which is the most
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474popular programming language.
1475
1476
1477
1478 vim:tw=78:ts=8:noet:ft=help:norl: