Bram Moolenaar | 6797782 | 2021-01-03 21:53:53 +0100 | [diff] [blame] | 1 | *vim9.txt* For Vim version 8.2. Last change: 2021 Jan 03 |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 8 | |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 9 | Vim9 script commands and expressions. *Vim9* *vim9* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 10 | |
| 11 | Most expression help is in |eval.txt|. This file is about the new syntax and |
| 12 | features in Vim9 script. |
| 13 | |
| 14 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 15 | |
| 16 | |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 17 | 1. What is Vim9 script? |Vim9-script| |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 18 | 2. Differences |vim9-differences| |
| 19 | 3. New style functions |fast-functions| |
| 20 | 4. Types |vim9-types| |
| 21 | 5. Namespace, Import and Export |vim9script| |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 22 | 6. Future work: classes |vim9-classes| |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 23 | |
| 24 | 9. Rationale |vim9-rationale| |
| 25 | |
| 26 | ============================================================================== |
| 27 | |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 28 | 1. What is Vim9 script? *Vim9-script* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 29 | |
| 30 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 31 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 32 | Vim script has been growing over time, while preserving backwards |
| 33 | compatibility. That means bad choices from the past often can't be changed |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 34 | and compatibility with Vi restricts possible solutions. Execution is quite |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 35 | slow, each line is parsed every time it is executed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 36 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 37 | The main goal of Vim9 script is to drastically improve performance. This is |
| 38 | accomplished by compiling commands into instructions that can be efficiently |
| 39 | executed. An increase in execution speed of 10 to 100 times can be expected. |
| 40 | |
| 41 | A secondary goal is to avoid Vim-specific constructs and get closer to |
| 42 | commonly used programming languages, such as JavaScript, TypeScript and Java. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 43 | |
| 44 | The performance improvements can only be achieved by not being 100% backwards |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 45 | compatible. For example, making function arguments available in the |
| 46 | "a:" dictionary adds quite a lot of overhead. In a Vim9 function this |
| 47 | dictionary is not available. Other differences are more subtle, such as how |
| 48 | errors are handled. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 49 | |
| 50 | The 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 Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 53 | - an autocommand defined in the context of the above |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 54 | |
| 55 | When using `:function` in a Vim9 script file the legacy syntax is used. |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 56 | However, this can be confusing and is therefore discouraged. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 57 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 58 | Vim9 script and legacy Vim script can be mixed. There is no requirement to |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 59 | rewrite old scripts, they keep working as before. You may want to use a few |
| 60 | `:def` functions for code that needs to be fast. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 61 | |
| 62 | ============================================================================== |
| 63 | |
| 64 | 2. Differences from legacy Vim script *vim9-differences* |
| 65 | |
| 66 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 67 | |
Bram Moolenaar | d58a3bf | 2020-09-28 21:48:16 +0200 | [diff] [blame] | 68 | Overview ~ |
| 69 | |
| 70 | Brief summary of the differences you will most often encounter when using Vim9 |
| 71 | script and `:def` functions; details are below: |
| 72 | - Comments start with #, not ": > |
| 73 | echo "hello" # comment |
| 74 | - Using a backslash for line continuation is hardly ever needed: > |
| 75 | echo "hello " |
| 76 | .. yourName |
| 77 | .. ", how are you?" |
| 78 | - White space is required in many places. |
| 79 | - Assign values without `:let`, declare variables with `:var`: > |
| 80 | var count = 0 |
| 81 | count += 3 |
| 82 | - Constants can be declared with `:final` and `:const`: > |
| 83 | final matches = [] # add matches |
| 84 | const names = ['Betty', 'Peter'] # cannot be changed |
| 85 | - `:final` cannot be used as an abbreviation of `:finally`. |
| 86 | - Variables and functions are script-local by default. |
| 87 | - Functions are declared with argument types and return type: > |
| 88 | def CallMe(count: number, message: string): bool |
| 89 | - Call functions without `:call`: > |
| 90 | writefile(['done'], 'file.txt') |
| 91 | - You cannot use `:xit`, `:t`, `:append`, `:change`, `:insert` or curly-braces |
| 92 | names. |
| 93 | - A range before a command must be prefixed with a colon: > |
| 94 | :%s/this/that |
| 95 | |
| 96 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 97 | Comments starting with # ~ |
| 98 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 99 | In legacy Vim script comments start with double quote. In Vim9 script |
| 100 | comments start with #. > |
| 101 | # declarations |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 102 | var count = 0 # number of occurrences |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 103 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 104 | The reason is that a double quote can also be the start of a string. In many |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 105 | places, especially halfway through an expression with a line break, it's hard |
| 106 | to tell what the meaning is, since both a string and a comment can be followed |
| 107 | by arbitrary text. To avoid confusion only # comments are recognized. This |
| 108 | is the same as in shell scripts and Python programs. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 109 | |
| 110 | In Vi # is a command to list text with numbers. In Vim9 script you can use |
| 111 | `:number` for that. > |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 112 | 101 number |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 113 | |
| 114 | To improve readability there must be a space between a command and the # |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 115 | that starts a comment: > |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 116 | var name = value # comment |
| 117 | var name = value# error! |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 118 | |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 119 | In legacy Vim script # is also used for the alternate file name. In Vim9 |
| 120 | script you need to use %% instead. Instead of ## use %%% (stands for all |
| 121 | arguments). |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 122 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 123 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | Vim9 functions ~ |
| 125 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 126 | A function defined with `:def` is compiled. Execution is many times faster, |
| 127 | often 10x to 100x times. |
| 128 | |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 129 | Many errors are already found when compiling, before the function is executed. |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 130 | The syntax is strict, to enforce code that is easy to read and understand. |
| 131 | |
Bram Moolenaar | 1b884a0 | 2020-12-10 21:11:27 +0100 | [diff] [blame] | 132 | Compilation is done when either of these is encountered: |
| 133 | - the first time the function is called |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 134 | - when the `:defcompile` command is encountered in the script where the |
| 135 | function was defined |
| 136 | - `:disassemble` is used for the function. |
| 137 | - a function that is compiled calls the function or uses it as a function |
| 138 | reference |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 139 | |
| 140 | `:def` has no options like `:function` does: "range", "abort", "dict" or |
Bram Moolenaar | 1b884a0 | 2020-12-10 21:11:27 +0100 | [diff] [blame] | 141 | "closure". A `:def` function always aborts on an error (unless `:silent!` was |
| 142 | used for the command or inside a `:try` block), does not get a range passed |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 143 | cannot be a "dict" function, and can always be a closure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 145 | The argument types and return type need to be specified. The "any" type can |
| 146 | be used, type checking will then be done at runtime, like with legacy |
| 147 | functions. |
| 148 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 149 | Arguments are accessed by name, without "a:", just like any other language. |
| 150 | There is no "a:" dictionary or "a:000" list. |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 151 | |
| 152 | Variable arguments are defined as the last argument, with a name and have a |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 153 | list type, similar to TypeScript. For example, a list of numbers: > |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 154 | def MyFunc(...itemlist: list<number>) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 155 | for item in itemlist |
| 156 | ... |
| 157 | |
| 158 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 159 | Functions and variables are script-local by default ~ |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 160 | *vim9-scopes* |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 161 | When using `:function` or `:def` to specify a new function at the script level |
| 162 | in a Vim9 script, the function is local to the script, as if "s:" was |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 163 | prefixed. Using the "s:" prefix is optional. To define a global function or |
| 164 | variable the "g:" prefix must be used. For functions in an autoload script |
| 165 | the "name#" prefix is sufficient. > |
Bram Moolenaar | ea2d8d2 | 2020-07-29 22:11:05 +0200 | [diff] [blame] | 166 | def ThisFunction() # script-local |
| 167 | def s:ThisFunction() # script-local |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 168 | def g:ThatFunction() # global |
Bram Moolenaar | ea2d8d2 | 2020-07-29 22:11:05 +0200 | [diff] [blame] | 169 | def scriptname#function() # autoload |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 170 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 171 | When using `:function` or `:def` to specify a nested function inside a `:def` |
| 172 | function, this nested function is local to the code block it is defined in. |
Bram Moolenaar | 4f4d51a | 2020-10-11 13:57:40 +0200 | [diff] [blame] | 173 | In a `:def` function it is not possible to define a script-local function. It |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 174 | is possible to define a global function by using the "g:" prefix. |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 175 | |
| 176 | When referring to a function and no "s:" or "g:" prefix is used, Vim will |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 177 | search for the function: |
Bram Moolenaar | 4f4d51a | 2020-10-11 13:57:40 +0200 | [diff] [blame] | 178 | - in the function scope, in block scopes |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 179 | - in the script scope, possibly imported |
| 180 | - in the list of global functions |
| 181 | However, it is recommended to always use "g:" to refer to a global function |
| 182 | for clarity. |
| 183 | |
| 184 | In all cases the function must be defined before used. That is when it is |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 185 | called, when `:defcompile` causes it to be compiled, or when code that calls |
| 186 | it is being compiled (to figure out the return type). |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 187 | |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 188 | The result is that functions and variables without a namespace can usually be |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 189 | found in the script, either defined there or imported. Global functions and |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 190 | variables could be defined anywhere (good luck finding out where!). |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 191 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 192 | Global functions can still be defined and deleted at nearly any time. In |
Bram Moolenaar | 2cfb4a2 | 2020-05-07 18:56:00 +0200 | [diff] [blame] | 193 | Vim9 script script-local functions are defined once when the script is sourced |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 194 | and cannot be deleted or replaced. |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 195 | |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 196 | When compiling a function and a function call is encountered for a function |
| 197 | that is not (yet) defined, the |FuncUndefined| autocommand is not triggered. |
| 198 | You can use an autoload function if needed, or call a legacy function and have |
| 199 | |FuncUndefined| triggered there. |
| 200 | |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 201 | |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 202 | Reloading a Vim9 script clears functions and variables by default ~ |
| 203 | *vim9-reload* |
| 204 | When loading a legacy Vim script a second time nothing is removed, the |
| 205 | commands will replace existing variables and functions and create new ones. |
| 206 | |
| 207 | When loading a Vim9 script a second time all existing script-local functions |
| 208 | and variables are deleted, thus you start with a clean slate. This is useful |
| 209 | if you are developing a plugin and want to try a new version. If you renamed |
| 210 | something you don't have to worry about the old name still hanging around. |
| 211 | |
| 212 | If you do want to keep items, use: > |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 213 | vim9script noclear |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 214 | |
| 215 | You want to use this in scripts that use a `finish` command to bail out at |
| 216 | some point when loaded again. E.g. when a buffer local option is set: > |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 217 | vim9script noclear |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 218 | setlocal completefunc=SomeFunc |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 219 | if exists('*g:SomeFunc') | finish | endif |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 220 | def g:SomeFunc() |
| 221 | .... |
| 222 | |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 223 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 224 | Variable declarations with :var, :final and :const ~ |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 225 | *vim9-declaration* *:var* |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 226 | Local variables need to be declared with `:var`. Local constants need to be |
| 227 | declared with `:final` or `:const`. We refer to both as "variables" in this |
| 228 | section. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 229 | |
| 230 | Variables can be local to a script, function or code block: > |
| 231 | vim9script |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 232 | var script_var = 123 |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 233 | def SomeFunc() |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 234 | var func_var = script_var |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 235 | if cond |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 236 | var block_var = func_var |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 237 | ... |
| 238 | |
| 239 | The variables are only visible in the block where they are defined and nested |
| 240 | blocks. Once the block ends the variable is no longer accessible: > |
| 241 | if cond |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 242 | var inner = 5 |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 243 | else |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 244 | var inner = 0 |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 245 | endif |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 246 | echo inner # Error! |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 247 | |
| 248 | The declaration must be done earlier: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 249 | var inner: number |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 250 | if cond |
| 251 | inner = 5 |
| 252 | else |
| 253 | inner = 0 |
| 254 | endif |
| 255 | echo inner |
| 256 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 257 | To intentionally hide a variable from code that follows, a block can be |
| 258 | used: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 259 | { |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 260 | var temp = 'temp' |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 261 | ... |
| 262 | } |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 263 | echo temp # Error! |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 264 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 265 | Declaring a variable with a type but without an initializer will initialize to |
| 266 | zero, false or empty. |
| 267 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 268 | In Vim9 script `:let` cannot be used. An existing variable is assigned to |
| 269 | without any command. The same for global, window, tab, buffer and Vim |
| 270 | variables, because they are not really declared. They can also be deleted |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 271 | with `:unlet`. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 272 | |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 273 | Variables and functions cannot shadow previously defined or imported variables |
| 274 | and functions. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 275 | Variables may shadow Ex commands, rename the variable if needed. |
| 276 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 277 | Global variables and user defined functions must be prefixed with "g:", also |
| 278 | at the script level. > |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 279 | vim9script |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 280 | var script_local = 'text' |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 281 | g:global = 'value' |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 282 | var Funcref = g:ThatFunction |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 283 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 284 | Since `&opt = value` is now assigning a value to option "opt", ":&" cannot be |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 285 | used to repeat a `:substitute` command. |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 286 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 287 | |
| 288 | Constants ~ |
| 289 | *vim9-const* *vim9-final* |
| 290 | How constants work varies between languages. Some consider a variable that |
| 291 | can't be assigned another value a constant. JavaScript is an example. Others |
| 292 | also make the value immutable, thus when a constant uses a list, the list |
| 293 | cannot be changed. In Vim9 we can use both. |
| 294 | |
| 295 | `:const` is used for making both the variable and the value a constant. Use |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 296 | this for composite structures that you want to make sure will not be modified. |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 297 | Example: > |
| 298 | const myList = [1, 2] |
| 299 | myList = [3, 4] # Error! |
| 300 | myList[0] = 9 # Error! |
| 301 | muList->add(3) # Error! |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 302 | < *:final* |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 303 | `:final` is used for making only the variable a constant, the value can be |
| 304 | changed. This is well known from Java. Example: > |
| 305 | final myList = [1, 2] |
| 306 | myList = [3, 4] # Error! |
| 307 | myList[0] = 9 # OK |
| 308 | muList->add(3) # OK |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 309 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 310 | It is common to write constants as ALL_CAPS, but you don't have to. |
| 311 | |
| 312 | The constant only applies to the value itself, not what it refers to. > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 313 | final females = ["Mary"] |
| 314 | const NAMES = [["John", "Peter"], females] |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 315 | NAMES[0] = ["Jack"] # Error! |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 316 | NAMES[0][0] = "Jack" # Error! |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 317 | NAMES[1] = ["Emma"] # Error! |
| 318 | Names[1][0] = "Emma" # OK, now females[0] == "Emma" |
| 319 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 320 | < *E1092* |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 321 | Declaring more than one variable at a time, using the unpack notation, is |
| 322 | currently not supported: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 323 | var [v1, v2] = GetValues() # Error! |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 324 | That is because the type needs to be inferred from the list item type, which |
| 325 | isn't that easy. |
| 326 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 327 | |
| 328 | Omitting :call and :eval ~ |
| 329 | |
| 330 | Functions can be called without `:call`: > |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 331 | writefile(lines, 'file') |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 332 | Using `:call` is still possible, but this is discouraged. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 333 | |
| 334 | A method call without `eval` is possible, so long as the start is an |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 335 | identifier or can't be an Ex command. Examples: > |
| 336 | myList->add(123) |
| 337 | g:myList->add(123) |
| 338 | [1, 2, 3]->Process() |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 339 | {a: 1, b: 2}->Process() |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 340 | "foobar"->Process() |
| 341 | ("foobar")->Process() |
| 342 | 'foobar'->Process() |
| 343 | ('foobar')->Process() |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 344 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 345 | In the rare case there is ambiguity between a function name and an Ex command, |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 346 | prepend ":" to make clear you want to use the Ex command. For example, there |
| 347 | is both the `:substitute` command and the `substitute()` function. When the |
| 348 | line starts with `substitute(` this will use the function. Prepend a colon to |
| 349 | use the command instead: > |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 350 | :substitute(pattern (replacement ( |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 351 | |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 352 | Note that while variables need to be defined before they can be used, |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 353 | functions can be called before being defined. This is required to allow |
| 354 | for cyclic dependencies between functions. It is slightly less efficient, |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 355 | since the function has to be looked up by name. And a typo in the function |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 356 | name will only be found when the function is called. |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 357 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 358 | |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 359 | Omitting function() ~ |
| 360 | |
| 361 | A user defined function can be used as a function reference in an expression |
| 362 | without `function()`. The argument types and return type will then be checked. |
| 363 | The function must already have been defined. > |
| 364 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 365 | var Funcref = MyFunction |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 366 | |
| 367 | When using `function()` the resulting type is "func", a function with any |
| 368 | number of arguments and any return type. The function can be defined later. |
| 369 | |
| 370 | |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 371 | Lambda using => instead of -> ~ |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 372 | |
| 373 | In legacy script there can be confusion between using "->" for a method call |
| 374 | and for a lambda. Also, when a "{" is found the parser needs to figure out if |
| 375 | it is the start of a lambda or a dictionary, which is now more complicated |
| 376 | because of the use of argument types. |
| 377 | |
| 378 | To avoid these problems Vim9 script uses a different syntax for a lambda, |
| 379 | which is similar to Javascript: > |
| 380 | var Lambda = (arg) => expression |
| 381 | |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 382 | No line break is allowed in the arguments of a lambda up to and including the |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 383 | "=>". This is OK: > |
| 384 | filter(list, (k, v) => |
| 385 | v > 0) |
| 386 | This does not work: > |
| 387 | filter(list, (k, v) |
| 388 | => v > 0) |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 389 | This also does not work: > |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 390 | filter(list, (k, |
| 391 | v) => v > 0) |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 392 | But you can use a backslash to concatenate the lines before parsing: > |
| 393 | filter(list, (k, |
| 394 | \ v) |
| 395 | \ => v > 0) |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 396 | |
| 397 | Additionally, a lambda can contain statements in {}: > |
| 398 | var Lambda = (arg) => { |
| 399 | g:was_called = 'yes' |
| 400 | return expression |
| 401 | } |
| 402 | NOT IMPLEMENTED YET |
| 403 | |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 404 | To avoid the "{" of a dictionary literal to be recognized as a statement block |
| 405 | wrap it in parenthesis: > |
| 406 | var Lambda = (arg) => ({key: 42}) |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 407 | |
| 408 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 409 | Automatic line continuation ~ |
| 410 | |
| 411 | In many cases it is obvious that an expression continues on the next line. In |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 412 | those cases there is no need to prefix the line with a backslash (see |
| 413 | |line-continuation|). For example, when a list spans multiple lines: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 414 | var mylist = [ |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 415 | 'one', |
| 416 | 'two', |
| 417 | ] |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 418 | And when a dict spans multiple lines: > |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 419 | var mydict = { |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 420 | one: 1, |
| 421 | two: 2, |
| 422 | } |
| 423 | Function call: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 424 | var result = Func( |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 425 | arg1, |
| 426 | arg2 |
| 427 | ) |
| 428 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 429 | For binary operators in expressions not in [], {} or () a line break is |
| 430 | possible just before or after the operator. For example: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 431 | var text = lead |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 432 | .. middle |
| 433 | .. end |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 434 | var total = start + |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 435 | end - |
| 436 | correction |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 437 | var result = positive |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 438 | ? PosFunc(arg) |
| 439 | : NegFunc(arg) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 440 | |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 441 | For a method call using "->" and a member using a dot, a line break is allowed |
| 442 | before it: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 443 | var result = GetBuilder() |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 444 | ->BuilderSetWidth(333) |
| 445 | ->BuilderSetHeight(777) |
| 446 | ->BuilderBuild() |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 447 | var result = MyDict |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 448 | .member |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 449 | |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 450 | For commands that have an argument that is a list of commands, the | character |
| 451 | at the start of the line indicates line continuation: > |
| 452 | autocmd BufNewFile *.match if condition |
| 453 | | echo 'match' |
| 454 | | endif |
| 455 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 456 | < *E1050* |
| 457 | To make it possible for the operator at the start of the line to be |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 458 | recognized, it is required to put a colon before a range. This will add |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 459 | "start" and print: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 460 | var result = start |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 461 | + print |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 462 | Like this: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 463 | var result = start + print |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 464 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 465 | This will assign "start" and print a line: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 466 | var result = start |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 467 | :+ print |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 468 | |
Bram Moolenaar | 23515b4 | 2020-11-29 14:36:24 +0100 | [diff] [blame] | 469 | Note that the colon is not required for the |+cmd| argument: > |
| 470 | edit +6 fname |
| 471 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 472 | It is also possible to split a function header over multiple lines, in between |
| 473 | arguments: > |
| 474 | def MyFunc( |
| 475 | text: string, |
| 476 | separator = '-' |
| 477 | ): string |
| 478 | |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 479 | Since a continuation line cannot be easily recognized the parsing of commands |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 480 | has been made stricter. E.g., because of the error in the first line, the |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 481 | second line is seen as a separate command: > |
| 482 | popup_create(some invalid expression, { |
| 483 | exit_cb: Func}) |
| 484 | Now "exit_cb: Func})" is actually a valid command: save any changes to the |
| 485 | file "_cb: Func})" and exit. To avoid this kind of mistake in Vim9 script |
| 486 | there must be white space between most command names and the argument. |
| 487 | |
| 488 | |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 489 | Notes: |
| 490 | - "enddef" cannot be used at the start of a continuation line, it ends the |
| 491 | current function. |
| 492 | - No line break is allowed in the LHS of an assignment. Specifically when |
| 493 | unpacking a list |:let-unpack|. This is OK: > |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 494 | [var1, var2] = |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 495 | Func() |
| 496 | < This does not work: > |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 497 | [var1, |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 498 | var2] = |
| 499 | Func() |
| 500 | - No line break is allowed in between arguments of an `:echo`, `:execute` and |
| 501 | similar commands. This is OK: > |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 502 | echo [1, |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 503 | 2] [3, |
| 504 | 4] |
| 505 | < This does not work: > |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 506 | echo [1, 2] |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 507 | [3, 4] |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 508 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 509 | No curly braces expansion ~ |
| 510 | |
| 511 | |curly-braces-names| cannot be used. |
| 512 | |
| 513 | |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 514 | Dictionary literals ~ |
| 515 | |
| 516 | Traditionally Vim has supported dictionary literals with a {} syntax: > |
| 517 | let dict = {'key': value} |
| 518 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 519 | Later it became clear that using a simple text key is very common, thus |
| 520 | literal dictionaries were introduced in a backwards compatible way: > |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 521 | let dict = #{key: value} |
| 522 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 523 | However, this #{} syntax is unlike any existing language. As it turns out |
| 524 | that using a literal key is much more common than using an expression, and |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 525 | considering that JavaScript uses this syntax, using the {} form for dictionary |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 526 | literals is considered a much more useful syntax. In Vim9 script the {} form |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 527 | uses literal keys: > |
| 528 | let dict = {key: value} |
| 529 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 530 | This works for alphanumeric characters, underscore and dash. If you want to |
| 531 | use another character, use a single or double quoted string: > |
| 532 | let dict = {'key with space': value} |
| 533 | let dict = {"key\twith\ttabs": value} |
| 534 | let dict = {'': value} # empty key |
| 535 | |
| 536 | In case the key needs to be an expression, square brackets can be used, just |
| 537 | like in JavaScript: > |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 538 | let dict = {["key" .. nr]: value} |
| 539 | |
| 540 | |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 541 | No :xit, :t, :append, :change or :insert ~ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 542 | |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 543 | These commands are too easily confused with local variable names. |
| 544 | Instead of `:x` or `:xit` you can use `:exit`. |
| 545 | Instead of `:t` you can use `:copy`. |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 546 | |
| 547 | |
| 548 | Comparators ~ |
| 549 | |
| 550 | The 'ignorecase' option is not used for comparators that use strings. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 551 | |
| 552 | |
| 553 | White space ~ |
| 554 | |
| 555 | Vim9 script enforces proper use of white space. This is no longer allowed: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 556 | var name=234 # Error! |
| 557 | var name= 234 # Error! |
| 558 | var name =234 # Error! |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 559 | There must be white space before and after the "=": > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 560 | var name = 234 # OK |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 561 | White space must also be put before the # that starts a comment after a |
| 562 | command: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 563 | var name = 234# Error! |
| 564 | var name = 234 # OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 565 | |
| 566 | White space is required around most operators. |
| 567 | |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 568 | White space is required in a sublist (list slice) around the ":", except at |
| 569 | the start and end: > |
| 570 | otherlist = mylist[v : count] # v:count has a different meaning |
| 571 | otherlist = mylist[:] # make a copy of the List |
| 572 | otherlist = mylist[v :] |
| 573 | otherlist = mylist[: v] |
| 574 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 575 | White space is not allowed: |
| 576 | - Between a function name and the "(": > |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 577 | call Func (arg) # Error! |
| 578 | call Func |
| 579 | \ (arg) # Error! |
| 580 | call Func(arg) # OK |
| 581 | call Func( |
| 582 | \ arg) # OK |
| 583 | call Func( |
| 584 | \ arg # OK |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 585 | \ ) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 586 | |
| 587 | |
| 588 | Conditions and expressions ~ |
| 589 | |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 590 | Conditions and expressions are mostly working like they do in other languages. |
| 591 | Some values are different from legacy Vim script: |
| 592 | value legacy Vim script Vim9 script ~ |
| 593 | 0 falsy falsy |
| 594 | 1 truthy truthy |
| 595 | 99 truthy Error! |
| 596 | "0" falsy Error! |
| 597 | "99" truthy Error! |
| 598 | "text" falsy Error! |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 599 | |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 600 | For the "??" operator and when using "!" then there is no error, every value |
| 601 | is either falsy or truthy. This is mostly like JavaScript, except that an |
| 602 | empty list and dict is falsy: |
| 603 | |
| 604 | type truthy when ~ |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 605 | bool true, v:true or 1 |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 606 | number non-zero |
| 607 | float non-zero |
| 608 | string non-empty |
| 609 | blob non-empty |
| 610 | list non-empty (different from JavaScript) |
| 611 | dictionary non-empty (different from JavaScript) |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 612 | func when there is a function name |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 613 | special true or v:true |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 614 | job when not NULL |
| 615 | channel when not NULL |
| 616 | class when not NULL |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 617 | object when not NULL (TODO: when isTrue() returns true) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 618 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 619 | The boolean operators "||" and "&&" expect the values to be boolean, zero or |
| 620 | one: > |
| 621 | 1 || false == true |
| 622 | 0 || 1 == true |
| 623 | 0 || false == false |
| 624 | 1 && true == true |
| 625 | 0 && 1 == false |
| 626 | 8 || 0 Error! |
| 627 | 'yes' && 0 Error! |
| 628 | [] || 99 Error! |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 629 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 630 | When using "!" for inverting, there is no error for using any type and the |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 631 | result is a boolean. "!!" can be used to turn any value into boolean: > |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 632 | !'yes' == false |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 633 | !![] == false |
| 634 | !![1, 2, 3] == true |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 635 | |
| 636 | When using "`.."` for string concatenation arguments of simple types are |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 637 | always converted to string: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 638 | 'hello ' .. 123 == 'hello 123' |
Bram Moolenaar | 7e6a515 | 2021-01-02 16:39:53 +0100 | [diff] [blame] | 639 | 'hello ' .. v:true == 'hello true' |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 640 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 641 | Simple types are string, float, special and bool. For other types |string()| |
| 642 | can be used. |
Bram Moolenaar | 6797782 | 2021-01-03 21:53:53 +0100 | [diff] [blame] | 643 | *false* *true* *null* |
| 644 | In Vim9 script one can use "true" for v:true, "false" for v:false and "null" |
| 645 | for v:null. When converting a boolean to a string "false" and "true" are |
| 646 | used, not "v:false" and "v:true" like in legacy script. "v:none" is not |
| 647 | changed, it is only used in JSON and has no equivalent in other languages. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 648 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 649 | Indexing a string with [idx] or [idx, idx] uses character indexes instead of |
| 650 | byte indexes. Example: > |
| 651 | echo 'bár'[1] |
| 652 | In legacy script this results in the character 0xc3 (an illegal byte), in Vim9 |
| 653 | script this results in the string 'á'. |
| 654 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 655 | |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 656 | What to watch out for ~ |
| 657 | *vim9-gotchas* |
| 658 | Vim9 was designed to be closer to often used programming languages, but at the |
| 659 | same time tries to support the legacy Vim commands. Some compromises had to |
| 660 | be made. Here is a summary of what might be unexpected. |
| 661 | |
| 662 | Ex command ranges need to be prefixed with a colon. > |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 663 | -> # legacy Vim: shifts the previous line to the right |
| 664 | ->func() # Vim9: method call in continuation line |
| 665 | :-> # Vim9: shifts the previous line to the right |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 666 | |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 667 | %s/a/b # legacy Vim: substitute on all lines |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 668 | x = alongname |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 669 | % another # Vim9: line continuation without a backslash |
| 670 | :%s/a/b # Vim9: substitute on all lines |
| 671 | 'text'->func() # Vim9: method call |
| 672 | :'t # legacy Vim: jump to mark m |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 673 | |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 674 | Some Ex commands can be confused with assignments in Vim9 script: > |
| 675 | g:name = value # assignment |
| 676 | g:pattern:cmd # invalid command - ERROR |
| 677 | :g:pattern:cmd # :global command |
| 678 | |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 679 | Functions defined with `:def` compile the whole function. Legacy functions |
| 680 | can bail out, and the following lines are not parsed: > |
| 681 | func Maybe() |
| 682 | if !has('feature') |
| 683 | return |
| 684 | endif |
| 685 | use-feature |
| 686 | endfunc |
| 687 | Vim9 functions are compiled as a whole: > |
| 688 | def Maybe() |
| 689 | if !has('feature') |
| 690 | return |
| 691 | endif |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 692 | use-feature # May give compilation error |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 693 | enddef |
| 694 | For a workaround, split it in two functions: > |
| 695 | func Maybe() |
| 696 | if has('feature') |
| 697 | call MaybyInner() |
| 698 | endif |
| 699 | endfunc |
| 700 | if has('feature') |
| 701 | def MaybeInner() |
| 702 | use-feature |
| 703 | enddef |
| 704 | endif |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 705 | Or put the unsupported code inside an `if` with a constant expression that |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 706 | evaluates to false: > |
| 707 | def Maybe() |
| 708 | if has('feature') |
| 709 | use-feature |
| 710 | endif |
| 711 | enddef |
| 712 | Note that for unrecognized commands there is no check for "|" and a following |
| 713 | command. This will give an error for missing `endif`: > |
| 714 | def Maybe() |
| 715 | if has('feature') | use-feature | endif |
| 716 | enddef |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 717 | |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 718 | Other differences ~ |
| 719 | |
| 720 | Patterns are used like 'magic' is set, unless explicitly overruled. |
| 721 | The 'edcompatible' option value is not used. |
| 722 | The 'gdefault' option value is not used. |
| 723 | |
| 724 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 725 | ============================================================================== |
| 726 | |
| 727 | 3. New style functions *fast-functions* |
| 728 | |
| 729 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 730 | |
| 731 | *:def* |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 732 | :def[!] {name}([arguments])[: {return-type}] |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 733 | Define a new function by the name {name}. The body of |
| 734 | the function follows in the next lines, until the |
| 735 | matching `:enddef`. |
| 736 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 737 | When {return-type} is omitted or is "void" the |
| 738 | function is not expected to return anything. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 739 | |
| 740 | {arguments} is a sequence of zero or more argument |
| 741 | declarations. There are three forms: |
| 742 | {name}: {type} |
| 743 | {name} = {value} |
| 744 | {name}: {type} = {value} |
| 745 | The first form is a mandatory argument, the caller |
| 746 | must always provide them. |
| 747 | The second and third form are optional arguments. |
| 748 | When the caller omits an argument the {value} is used. |
| 749 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 750 | The function will be compiled into instructions when |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 751 | called, or when `:disassemble` or `:defcompile` is |
| 752 | used. Syntax and type errors will be produced at that |
| 753 | time. |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 754 | |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 755 | It is possible to nest `:def` inside another `:def` or |
| 756 | `:function` up to about 50 levels deep. |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 757 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 758 | [!] is used as with `:function`. Note that |
| 759 | script-local functions cannot be deleted or redefined |
| 760 | later in Vim9 script. They can only be removed by |
| 761 | reloading the same script. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 762 | |
| 763 | *:enddef* |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 764 | :enddef End of a function defined with `:def`. It should be on |
| 765 | a line by its own. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 766 | |
| 767 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 768 | If the script the function is defined in is Vim9 script, then script-local |
| 769 | variables can be accessed without the "s:" prefix. They must be defined |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 770 | before the function is compiled. If the script the function is defined in is |
| 771 | legacy script, then script-local variables must be accessed with the "s:" |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 772 | prefix and they do not need to exist (they can be deleted any time). |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 773 | |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 774 | *:defc* *:defcompile* |
| 775 | :defc[ompile] Compile functions defined in the current script that |
| 776 | were not compiled yet. |
| 777 | This will report errors found during the compilation. |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 778 | |
Bram Moolenaar | ebdf3c9 | 2020-02-15 21:41:42 +0100 | [diff] [blame] | 779 | *:disa* *:disassemble* |
| 780 | :disa[ssemble] {func} Show the instructions generated for {func}. |
| 781 | This is for debugging and testing. |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 782 | Note that for command line completion of {func} you |
| 783 | can prepend "s:" to find script-local functions. |
Bram Moolenaar | ebdf3c9 | 2020-02-15 21:41:42 +0100 | [diff] [blame] | 784 | |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 785 | Limitations ~ |
| 786 | |
| 787 | Local variables will not be visible to string evaluation. For example: > |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 788 | def MapList(): list<string> |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 789 | var list = ['aa', 'bb', 'cc', 'dd'] |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 790 | return range(1, 2)->map('list[v:val]') |
| 791 | enddef |
| 792 | |
| 793 | The map argument is a string expression, which is evaluated without the |
| 794 | function scope. Instead, use a lambda: > |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 795 | def MapList(): list<string> |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 796 | var list = ['aa', 'bb', 'cc', 'dd'] |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 797 | return range(1, 2)->map(( _, v) => list[v]) |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 798 | enddef |
| 799 | |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 800 | The same is true for commands that are not compiled, such as `:global`. |
| 801 | For these the backtick expansion can be used. Example: > |
| 802 | def Replace() |
| 803 | var newText = 'blah' |
| 804 | g/pattern/s/^/`=newText`/ |
| 805 | enddef |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 806 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 807 | ============================================================================== |
| 808 | |
| 809 | 4. Types *vim9-types* |
| 810 | |
| 811 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 812 | |
| 813 | The following builtin types are supported: |
| 814 | bool |
| 815 | number |
| 816 | float |
| 817 | string |
| 818 | blob |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 819 | list<{type}> |
| 820 | dict<{type}> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 821 | job |
| 822 | channel |
Bram Moolenaar | b17893a | 2020-03-14 08:19:51 +0100 | [diff] [blame] | 823 | func |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 824 | func: {type} |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 825 | func({type}, ...) |
| 826 | func({type}, ...): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 827 | |
| 828 | Not supported yet: |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 829 | tuple<a: {type}, b: {type}, ...> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 830 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 831 | These types can be used in declarations, but no value will have this type: |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 832 | {type}|{type} {not implemented yet} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 833 | void |
| 834 | any |
| 835 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 836 | There is no array type, use list<{type}> instead. For a list constant an |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 837 | efficient implementation is used that avoids allocating lot of small pieces of |
| 838 | memory. |
| 839 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 840 | A partial and function can be declared in more or less specific ways: |
| 841 | func any kind of function reference, no type |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 842 | checking for arguments or return value |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 843 | func: {type} any number and type of arguments with specific |
| 844 | return type |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 845 | func({type}) function with argument type, does not return |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 846 | a value |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 847 | func({type}): {type} function with argument type and return type |
| 848 | func(?{type}) function with type of optional argument, does |
| 849 | not return a value |
| 850 | func(...{type}) function with type of variable number of |
| 851 | arguments, does not return a value |
| 852 | func({type}, ?{type}, ...{type}): {type} |
| 853 | function with: |
| 854 | - type of mandatory argument |
| 855 | - type of optional argument |
| 856 | - type of variable number of arguments |
| 857 | - return type |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 858 | |
| 859 | If the return type is "void" the function does not return a value. |
| 860 | |
| 861 | The reference can also be a |Partial|, in which case it stores extra arguments |
| 862 | and/or a dictionary, which are not visible to the caller. Since they are |
| 863 | called in the same way the declaration is the same. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 864 | |
| 865 | Custom types can be defined with `:type`: > |
| 866 | :type MyList list<string> |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 867 | Custom types must start with a capital letter, to avoid name clashes with |
| 868 | builtin types added later, similarly to user functions. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 869 | {not implemented yet} |
| 870 | |
| 871 | And classes and interfaces can be used as types: > |
| 872 | :class MyClass |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 873 | :var mine: MyClass |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | |
| 875 | :interface MyInterface |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 876 | :var mine: MyInterface |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 877 | |
| 878 | :class MyTemplate<Targ> |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 879 | :var mine: MyTemplate<number> |
| 880 | :var mine: MyTemplate<string> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 881 | |
| 882 | :class MyInterface<Targ> |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 883 | :var mine: MyInterface<number> |
| 884 | :var mine: MyInterface<string> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 885 | {not implemented yet} |
| 886 | |
| 887 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 888 | Variable types and type casting ~ |
| 889 | *variable-types* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 890 | Variables declared in Vim9 script or in a `:def` function have a type, either |
| 891 | specified explicitly or inferred from the initialization. |
| 892 | |
| 893 | Global, buffer, window and tab page variables do not have a specific type, the |
| 894 | value can be changed at any time, possibly changing the type. Therefore, in |
| 895 | compiled code the "any" type is assumed. |
| 896 | |
| 897 | This can be a problem when the "any" type is undesired and the actual type is |
| 898 | expected to always be the same. For example, when declaring a list: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 899 | var l: list<number> = [1, g:two] |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 900 | At compile time Vim doesn't know the type of "g:two" and the expression type |
| 901 | becomes list<any>. An instruction is generated to check the list type before |
| 902 | doing the assignment, which is a bit inefficient. |
| 903 | *type-casting* |
| 904 | To avoid this, use a type cast: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 905 | var l: list<number> = [1, <number>g:two] |
Bram Moolenaar | 4072ba5 | 2020-12-23 13:56:35 +0100 | [diff] [blame] | 906 | The compiled code will then only check that "g:two" is a number and give an |
| 907 | error if it isn't. This is called type casting. |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 908 | |
| 909 | The syntax of a type cast is: "<" {type} ">". There cannot be white space |
| 910 | after the "<" or before the ">" (to avoid them being confused with |
| 911 | smaller-than and bigger-than operators). |
| 912 | |
| 913 | The semantics is that, if needed, a runtime type check is performed. The |
| 914 | value is not actually changed. If you need to change the type, e.g. to change |
| 915 | it to a string, use the |string()| function. Or use |str2nr()| to convert a |
| 916 | string to a number. |
| 917 | |
| 918 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 919 | Type inference ~ |
| 920 | *type-inference* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 921 | In general: Whenever the type is clear it can be omitted. For example, when |
| 922 | declaring a variable and giving it a value: > |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 923 | var name = 0 # infers number type |
| 924 | var name = 'hello' # infers string type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 925 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 926 | The type of a list and dictionary comes from the common type of the values. |
| 927 | If the values all have the same type, that type is used for the list or |
| 928 | dictionary. If there is a mix of types, the "any" type is used. > |
| 929 | [1, 2, 3] list<number> |
| 930 | ['a', 'b', 'c'] list<string> |
| 931 | [1, 'x', 3] list<any> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 932 | |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 933 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 934 | Stricter type checking ~ |
| 935 | *type-checking* |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 936 | In legacy Vim script, where a number was expected, a string would be |
| 937 | automatically converted to a number. This was convenient for an actual number |
| 938 | such as "123", but leads to unexpected problems (but no error message) if the |
| 939 | string doesn't start with a number. Quite often this leads to hard-to-find |
| 940 | bugs. |
| 941 | |
| 942 | In Vim9 script this has been made stricter. In most places it works just as |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 943 | before, if the value used matches the expected type. There will sometimes be |
| 944 | an error, thus breaking backwards compatibility. For example: |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 945 | - Using a number other than 0 or 1 where a boolean is expected. *E1023* |
| 946 | - Using a string value when setting a number options. |
| 947 | - Using a number where a string is expected. *E1024* |
| 948 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 949 | ============================================================================== |
| 950 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 951 | 5. Namespace, Import and Export |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 952 | *vim9script* *vim9-export* *vim9-import* |
| 953 | |
| 954 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 955 | |
| 956 | A Vim9 script can be written to be imported. This means that everything in |
| 957 | the script is local, unless exported. Those exported items, and only those |
| 958 | items, can then be imported in another script. |
| 959 | |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 960 | You can cheat by using the global namespace explicitly. We will assume here |
| 961 | that you don't do that. |
| 962 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 963 | |
| 964 | Namespace ~ |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 965 | *vim9-namespace* |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 966 | To recognize a file that can be imported the `vim9script` statement must |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 967 | appear as the first statement in the file. It tells Vim to interpret the |
| 968 | script in its own namespace, instead of the global namespace. If a file |
| 969 | starts with: > |
| 970 | vim9script |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 971 | var myvar = 'yes' |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 972 | Then "myvar" will only exist in this file. While without `vim9script` it would |
| 973 | be available as `g:myvar` from any other script and function. |
| 974 | |
| 975 | The variables at the file level are very much like the script-local "s:" |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 976 | variables in legacy Vim script, but the "s:" is omitted. And they cannot be |
| 977 | deleted. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 978 | |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 979 | In Vim9 script the global "g:" namespace can still be used as before. And the |
| 980 | "w:", "b:" and "t:" namespaces. These have in common that variables are not |
| 981 | declared and they can be deleted. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 982 | |
| 983 | A side effect of `:vim9script` is that the 'cpoptions' option is set to the |
| 984 | Vim default value, like with: > |
| 985 | :set cpo&vim |
| 986 | One of the effects is that |line-continuation| is always enabled. |
| 987 | The original value of 'cpoptions' is restored at the end of the script. |
| 988 | |
| 989 | |
| 990 | Export ~ |
| 991 | *:export* *:exp* |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 992 | Exporting an item can be written as: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 993 | export const EXPORTED_CONST = 1234 |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 994 | export var someValue = ... |
| 995 | export final someValue = ... |
| 996 | export const someValue = ... |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 997 | export def MyFunc() ... |
| 998 | export class MyClass ... |
| 999 | |
| 1000 | As this suggests, only constants, variables, `:def` functions and classes can |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 1001 | be exported. {classes are not implemented yet} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1002 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1003 | *E1042* |
| 1004 | `:export` can only be used in Vim9 script, at the script level. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1005 | |
| 1006 | |
| 1007 | Import ~ |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 1008 | *:import* *:imp* *E1094* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1009 | The exported items can be imported individually in another Vim9 script: > |
| 1010 | import EXPORTED_CONST from "thatscript.vim" |
| 1011 | import MyClass from "myclass.vim" |
| 1012 | |
| 1013 | To import multiple items at the same time: > |
| 1014 | import {someValue, MyClass} from "thatscript.vim" |
| 1015 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 1016 | In case the name is ambiguous, another name can be specified: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1017 | import MyClass as ThatClass from "myclass.vim" |
| 1018 | import {someValue, MyClass as ThatClass} from "myclass.vim" |
| 1019 | |
| 1020 | To import all exported items under a specific identifier: > |
| 1021 | import * as That from 'thatscript.vim' |
| 1022 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1023 | {not implemented yet: using "This as That"} |
| 1024 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1025 | Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free |
| 1026 | to choose the name "That", but it is highly recommended to use the name of the |
| 1027 | script file to avoid confusion. |
| 1028 | |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1029 | `:import` can also be used in legacy Vim script. The imported items still |
| 1030 | become script-local, even when the "s:" prefix is not given. |
| 1031 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1032 | The script name after `import` can be: |
| 1033 | - A relative path, starting "." or "..". This finds a file relative to the |
| 1034 | location of the script file itself. This is useful to split up a large |
| 1035 | plugin into several files. |
| 1036 | - An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 1037 | will rarely be used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1038 | - A path not being relative or absolute. This will be found in the |
| 1039 | "import" subdirectories of 'runtimepath' entries. The name will usually be |
| 1040 | longer and unique, to avoid loading the wrong file. |
| 1041 | |
| 1042 | Once a vim9 script file has been imported, the result is cached and used the |
| 1043 | next time the same script is imported. It will not be read again. |
| 1044 | *:import-cycle* |
| 1045 | The `import` commands are executed when encountered. If that script (directly |
| 1046 | or indirectly) imports the current script, then items defined after the |
| 1047 | `import` won't be processed yet. Therefore cyclic imports can exist, but may |
| 1048 | result in undefined items. |
| 1049 | |
| 1050 | |
| 1051 | Import in an autoload script ~ |
| 1052 | |
| 1053 | For optimal startup speed, loading scripts should be postponed until they are |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 1054 | actually needed. A recommended mechanism: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1055 | |
| 1056 | 1. In the plugin define user commands, functions and/or mappings that refer to |
| 1057 | an autoload script. > |
| 1058 | command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>) |
| 1059 | |
| 1060 | < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. |
| 1061 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 1062 | 2. In the autoload script do the actual work. You can import items from |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1063 | other files to split up functionality in appropriate pieces. > |
| 1064 | vim9script |
| 1065 | import FilterFunc from "../import/someother.vim" |
| 1066 | def searchfor#Stuff(arg: string) |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1067 | var filtered = FilterFunc(arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1068 | ... |
| 1069 | < This goes in .../autoload/searchfor.vim. "searchfor" in the file name |
| 1070 | must be exactly the same as the prefix for the function name, that is how |
| 1071 | Vim finds the file. |
| 1072 | |
| 1073 | 3. Other functionality, possibly shared between plugins, contains the exported |
| 1074 | items and any private items. > |
| 1075 | vim9script |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1076 | var localVar = 'local' |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1077 | export def FilterFunc(arg: string): string |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1078 | ... |
| 1079 | < This goes in .../import/someother.vim. |
| 1080 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 1081 | When compiling a `:def` function and a function in an autoload script is |
| 1082 | encountered, the script is not loaded until the `:def` function is called. |
| 1083 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1084 | |
| 1085 | Import in legacy Vim script ~ |
| 1086 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1087 | If an `import` statement is used in legacy Vim script, the script-local "s:" |
| 1088 | namespace will be used for the imported item, even when "s:" is not specified. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1089 | |
| 1090 | |
| 1091 | ============================================================================== |
| 1092 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1093 | 6. Future work: classes *vim9-classes* |
| 1094 | |
| 1095 | Above "class" was mentioned a few times, but it has not been implemented yet. |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1096 | Most of Vim9 script can be created without this functionality, and since |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1097 | implementing classes is going to be a lot of work, it is left for the future. |
| 1098 | For now we'll just make sure classes can be added later. |
| 1099 | |
| 1100 | Thoughts: |
| 1101 | - `class` / `endclass`, everything in one file |
| 1102 | - Class names are always CamelCase |
| 1103 | - Single constructor |
| 1104 | - Single inheritance with `class ThisClass extends BaseClass` |
| 1105 | - `abstract class` |
| 1106 | - `interface` (Abstract class without any implementation) |
| 1107 | - `class SomeClass implements SomeInterface` |
| 1108 | - Generics for class: `class <Tkey, Tentry>` |
| 1109 | - Generics for function: `def <Tkey> GetLast(key: Tkey)` |
| 1110 | |
| 1111 | Again, much of this is from TypeScript. |
| 1112 | |
| 1113 | Some things that look like good additions: |
| 1114 | - Use a class as an interface (like Dart) |
| 1115 | - Extend a class with methods, using an import (like Dart) |
| 1116 | |
| 1117 | An important class that will be provided is "Promise". Since Vim is single |
| 1118 | threaded, connecting asynchronous operations is a natural way of allowing |
| 1119 | plugins to do their work without blocking the user. It's a uniform way to |
| 1120 | invoke callbacks and handle timeouts and errors. |
| 1121 | |
| 1122 | ============================================================================== |
| 1123 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1124 | 9. Rationale *vim9-rationale* |
| 1125 | |
| 1126 | The :def command ~ |
| 1127 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1128 | Plugin writers have asked for much faster Vim script. Investigations have |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 1129 | shown that keeping the existing semantics of function calls make this close to |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1130 | impossible, because of the overhead involved with calling a function, setting |
| 1131 | up the local function scope and executing lines. There are many details that |
| 1132 | need to be handled, such as error messages and exceptions. The need to create |
| 1133 | a dictionary for a: and l: scopes, the a:000 list and several others add too |
| 1134 | much overhead that cannot be avoided. |
| 1135 | |
| 1136 | Therefore the `:def` method to define a new-style function had to be added, |
| 1137 | which allows for a function with different semantics. Most things still work |
| 1138 | as before, but some parts do not. A new way to define a function was |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1139 | considered the best way to separate the legacy style code from Vim9 style code. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1140 | |
| 1141 | Using "def" to define a function comes from Python. Other languages use |
| 1142 | "function" which clashes with legacy Vim script. |
| 1143 | |
| 1144 | |
| 1145 | Type checking ~ |
| 1146 | |
| 1147 | When compiling lines of Vim commands into instructions as much as possible |
| 1148 | should be done at compile time. Postponing it to runtime makes the execution |
| 1149 | slower and means mistakes are found only later. For example, when |
| 1150 | encountering the "+" character and compiling this into a generic add |
| 1151 | instruction, at execution time the instruction would have to inspect the type |
| 1152 | of the arguments and decide what kind of addition to do. And when the |
| 1153 | type is dictionary throw an error. If the types are known to be numbers then |
| 1154 | an "add number" instruction can be used, which is faster. The error can be |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1155 | given at compile time, no error handling is needed at runtime, since adding |
| 1156 | two numbers cannot fail. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1157 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1158 | The syntax for types, using <type> for compound types, is similar to Java. It |
| 1159 | is easy to understand and widely used. The type names are what were used in |
| 1160 | Vim before, with some additions such as "void" and "bool". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1161 | |
| 1162 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1163 | Removing clutter and weirdness ~ |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1164 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1165 | Once decided that `:def` functions have different syntax than legacy functions, |
| 1166 | we are free to add improvements to make the code more familiar for users who |
| 1167 | know popular programming languages. In other words: remove weird things that |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1168 | only Vim does. |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1169 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1170 | We can also remove clutter, mainly things that were done to make Vim script |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1171 | backwards compatible with the good old Vi commands. |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1172 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1173 | Examples: |
| 1174 | - Drop `:call` for calling a function and `:eval` for manipulating data. |
| 1175 | - Drop using a leading backslash for line continuation, automatically figure |
| 1176 | out where an expression ends. |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1177 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1178 | However, this does require that some things need to change: |
| 1179 | - Comments start with # instead of ", to avoid confusing them with strings. |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1180 | This is good anyway, it is known from several popular languages. |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1181 | - Ex command ranges need to be prefixed with a colon, to avoid confusion with |
| 1182 | expressions (single quote can be a string or a mark, "/" can be divide or a |
| 1183 | search command, etc.). |
| 1184 | |
| 1185 | Goal is to limit the differences. A good criteria is that when the old syntax |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1186 | is accidentally used you are very likely to get an error message. |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1187 | |
| 1188 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1189 | Syntax and semantics from popular languages ~ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1190 | |
| 1191 | Script writers have complained that the Vim script syntax is unexpectedly |
| 1192 | different from what they are used to. To reduce this complaint popular |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1193 | languages are used as an example. At the same time, we do not want to abandon |
| 1194 | the well-known parts of legacy Vim script. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1195 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1196 | For many things TypeScript is followed. It's a recent language that is |
| 1197 | gaining popularity and has similarities with Vim script. It also has a |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1198 | mix of static typing (a variable always has a known value type) and dynamic |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1199 | typing (a variable can have different types, this changes at runtime). Since |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1200 | legacy Vim script is dynamically typed and a lot of existing functionality |
| 1201 | (esp. builtin functions) depends on that, while static typing allows for much |
| 1202 | faster execution, we need to have this mix in Vim9 script. |
| 1203 | |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1204 | There is no intention to completely match TypeScript syntax and semantics. We |
| 1205 | just want to take those parts that we can use for Vim and we expect Vim users |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1206 | will be happy with. TypeScript is a complex language with its own history, |
| 1207 | advantages and disadvantages. To get an idea of the disadvantages read the |
| 1208 | book: "JavaScript: The Good Parts". Or find the article "TypeScript: the good |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1209 | parts" and read the "Things to avoid" section. |
| 1210 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1211 | People familiar with other languages (Java, Python, etc.) will also find |
| 1212 | things in TypeScript that they do not like or do not understand. We'll try to |
| 1213 | avoid those things. |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1214 | |
| 1215 | Specific items from TypeScript we avoid: |
| 1216 | - Overloading "+", using it both for addition and string concatenation. This |
| 1217 | goes against legacy Vim script and often leads to mistakes. For that reason |
| 1218 | we will keep using ".." for string concatenation. Lua also uses ".." this |
| 1219 | way. And it allows for conversion to string for more values. |
| 1220 | - TypeScript can use an expression like "99 || 'yes'" in a condition, but |
| 1221 | cannot assign the value to a boolean. That is inconsistent and can be |
| 1222 | annoying. Vim recognizes an expression with && or || and allows using the |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1223 | result as a bool. TODO: to be reconsidered |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1224 | - TypeScript considers an empty string as Falsy, but an empty list or dict as |
| 1225 | Truthy. That is inconsistent. In Vim an empty list and dict are also |
| 1226 | Falsy. |
| 1227 | - TypeScript has various "Readonly" types, which have limited usefulness, |
| 1228 | since a type cast can remove the immutable nature. Vim locks the value, |
| 1229 | which is more flexible, but is only checked at runtime. |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1230 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1231 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1232 | Declarations ~ |
| 1233 | |
| 1234 | Legacy Vim script uses `:let` for every assignment, while in Vim9 declarations |
| 1235 | are used. That is different, thus it's good to use a different command: |
| 1236 | `:var`. This is used in many languages. The semantics might be slightly |
| 1237 | different, but it's easily recognized as a declaration. |
| 1238 | |
Bram Moolenaar | 23515b4 | 2020-11-29 14:36:24 +0100 | [diff] [blame] | 1239 | Using `:const` for constants is common, but the semantics varies. Some |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1240 | languages only make the variable immutable, others also make the value |
| 1241 | immutable. Since "final" is well known from Java for only making the variable |
| 1242 | immutable we decided to use that. And then `:const` can be used for making |
| 1243 | both immutable. This was also used in legacy Vim script and the meaning is |
| 1244 | almost the same. |
| 1245 | |
| 1246 | What we end up with is very similar to Dart: > |
| 1247 | :var name # mutable variable and value |
| 1248 | :final name # immutable variable, mutable value |
| 1249 | :const name # immutable variable and value |
| 1250 | |
| 1251 | Since legacy and Vim9 script will be mixed and global variables will be |
| 1252 | shared, optional type checking is desirable. Also, type inference will avoid |
| 1253 | the need for specifying the type in many cases. The TypeScript syntax fits |
| 1254 | best for adding types to declarations: > |
| 1255 | var name: string # string type is specified |
| 1256 | ... |
| 1257 | name = 'John' |
| 1258 | const greeting = 'hello' # string type is inferred |
| 1259 | |
| 1260 | This is how we put types in a declaration: > |
| 1261 | var mylist: list<string> |
| 1262 | final mylist: list<string> = ['foo'] |
| 1263 | def Func(arg1: number, arg2: string): bool |
| 1264 | |
| 1265 | Two alternatives were considered: |
| 1266 | 1. Put the type before the name, like Dart: > |
| 1267 | var list<string> mylist |
| 1268 | final list<string> mylist = ['foo'] |
| 1269 | def Func(number arg1, string arg2) bool |
| 1270 | 2. Put the type after the variable name, but do not use a colon, like Go: > |
| 1271 | var mylist list<string> |
| 1272 | final mylist list<string> = ['foo'] |
| 1273 | def Func(arg1 number, arg2 string) bool |
| 1274 | |
| 1275 | The first is more familiar for anyone used to C or Java. The second one |
Bram Moolenaar | 4f4d51a | 2020-10-11 13:57:40 +0200 | [diff] [blame] | 1276 | doesn't really have an advantage over the first, so let's discard the second. |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1277 | |
| 1278 | Since we use type inference the type can be left out when it can be inferred |
| 1279 | from the value. This means that after `var` we don't know if a type or a name |
| 1280 | follows. That makes parsing harder, not only for Vim but also for humans. |
| 1281 | Also, it will not be allowed to use a variable name that could be a type name, |
| 1282 | using `var string string` is too confusing. |
| 1283 | |
| 1284 | The chosen syntax, using a colon to separate the name from the type, adds |
| 1285 | punctuation, but it actually makes it easier to recognize the parts of a |
| 1286 | declaration. |
| 1287 | |
| 1288 | |
| 1289 | Expressions ~ |
| 1290 | |
Bram Moolenaar | 4f4d51a | 2020-10-11 13:57:40 +0200 | [diff] [blame] | 1291 | Expression evaluation was already close to what other languages are doing. |
| 1292 | Some details are unexpected and can be improved. For example a boolean |
| 1293 | condition would accept a string, convert it to a number and check if the |
| 1294 | number is non-zero. This is unexpected and often leads to mistakes, since |
| 1295 | text not starting with a number would be converted to zero, which is |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 1296 | considered false. Thus using a string for a condition would often not give an |
| 1297 | error and be considered false. That is confusing. |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1298 | |
Bram Moolenaar | 23515b4 | 2020-11-29 14:36:24 +0100 | [diff] [blame] | 1299 | In Vim9 type checking is stricter to avoid mistakes. Where a condition is |
Bram Moolenaar | 4f4d51a | 2020-10-11 13:57:40 +0200 | [diff] [blame] | 1300 | used, e.g. with the `:if` command and the `||` operator, only boolean-like |
| 1301 | values are accepted: |
| 1302 | true: `true`, `v:true`, `1`, `0 < 9` |
| 1303 | false: `false`, `v:false`, `0`, `0 > 9` |
| 1304 | Note that the number zero is false and the number one is true. This is more |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 1305 | permissive than most other languages. It was done because many builtin |
Bram Moolenaar | 4f4d51a | 2020-10-11 13:57:40 +0200 | [diff] [blame] | 1306 | functions return these values. |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1307 | |
Bram Moolenaar | 4f4d51a | 2020-10-11 13:57:40 +0200 | [diff] [blame] | 1308 | If you have any type of value and want to use it as a boolean, use the `!!` |
| 1309 | operator: |
| 1310 | true: !`!'text'`, `!![99]`, `!!{'x': 1}`, `!!99` |
| 1311 | false: `!!''`, `!![]`, `!!{}` |
| 1312 | |
| 1313 | From a language like JavaScript we have this handy construct: > |
| 1314 | GetName() || 'unknown' |
| 1315 | However, this conflicts with only allowing a boolean for a condition. |
| 1316 | Therefore the "??" operator was added: > |
| 1317 | GetName() ?? 'unknown' |
| 1318 | Here you can explicitly express your intention to use the value as-is and not |
| 1319 | result in a boolean. This is called the |falsy-operator|. |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1320 | |
| 1321 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1322 | Import and Export ~ |
| 1323 | |
| 1324 | A problem of legacy Vim script is that by default all functions and variables |
| 1325 | are global. It is possible to make them script-local, but then they are not |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1326 | available in other scripts. This defies the concept of a package that only |
| 1327 | exports selected items and keeps the rest local. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1328 | |
Bram Moolenaar | 3d1cde8 | 2020-08-15 18:55:18 +0200 | [diff] [blame] | 1329 | In Vim9 script a mechanism very similar to the JavaScript import and export |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1330 | mechanism is supported. It is a variant to the existing `:source` command |
| 1331 | that works like one would expect: |
| 1332 | - Instead of making everything global by default, everything is script-local, |
| 1333 | unless exported. |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1334 | - When importing a script the symbols that are imported are explicitly listed, |
| 1335 | avoiding name conflicts and failures if functionality is added later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1336 | - The mechanism allows for writing a big, long script with a very clear API: |
| 1337 | the exported function(s) and class(es). |
| 1338 | - By using relative paths loading can be much faster for an import inside of a |
| 1339 | package, no need to search many directories. |
| 1340 | - Once an import has been used, it can be cached and loading it again can be |
| 1341 | avoided. |
| 1342 | - The Vim-specific use of "s:" to make things script-local can be dropped. |
| 1343 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1344 | When sourcing a Vim9 script from a legacy script, only the items defined |
| 1345 | globally can be used, not the exported items. Alternatives considered: |
| 1346 | - All the exported items become available as script-local items. This makes |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1347 | it uncontrollable what items get defined and likely soon leads to trouble. |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1348 | - Use the exported items and make them global. Disadvantage is that it's then |
| 1349 | not possible to avoid name clashes in the global namespace. |
| 1350 | - Completely disallow sourcing a Vim9 script, require using `:import`. That |
| 1351 | makes it difficult to use scripts for testing, or sourcing them from the |
| 1352 | command line to try them out. |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1353 | Note that you can also use `:import` in legacy Vim script, see above. |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 1354 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1355 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1356 | Compiling functions early ~ |
| 1357 | |
| 1358 | Functions are compiled when called or when `:defcompile` is used. Why not |
| 1359 | compile them early, so that syntax and type errors are reported early? |
| 1360 | |
| 1361 | The functions can't be compiled right away when encountered, because there may |
| 1362 | be forward references to functions defined later. Consider defining functions |
| 1363 | A, B and C, where A calls B, B calls C, and C calls A again. It's impossible |
| 1364 | to reorder the functions to avoid forward references. |
| 1365 | |
| 1366 | An alternative would be to first scan through the file to locate items and |
| 1367 | figure out their type, so that forward references are found, and only then |
| 1368 | execute the script and compile the functions. This means the script has to be |
| 1369 | parsed twice, which is slower, and some conditions at the script level, such |
| 1370 | as checking if a feature is supported, are hard to use. An attempt was made |
| 1371 | to see if it works, but it turned out to be impossible to make work nicely. |
| 1372 | |
| 1373 | It would be possible to compile all the functions at the end of the script. |
| 1374 | The drawback is that if a function never gets called, the overhead of |
| 1375 | compiling it counts anyway. Since startup speed is very important, in most |
| 1376 | cases it's better to do it later and accept that syntax and type errors are |
| 1377 | only reported then. In case these errors should be found early, e.g. when |
| 1378 | testing, the `:defcompile` command will help out. |
| 1379 | |
| 1380 | |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 1381 | Why not use an embedded language? ~ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1382 | |
| 1383 | Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1384 | these interfaces have never become widely used, for various reasons. When |
| 1385 | Vim9 was designed a decision was made to make these interfaces lower priority |
| 1386 | and concentrate on Vim script. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1387 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1388 | Still, plugin writers may find other languages more familiar, want to use |
| 1389 | existing libraries or see a performance benefit. We encourage plugin authors |
| 1390 | to write code in any language and run it as an external tool, using jobs and |
| 1391 | channels. We can try to make this easier somehow. |
| 1392 | |
| 1393 | Using an external tool also has disadvantages. An alternative is to convert |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1394 | the tool into Vim script. For that to be possible without too much |
| 1395 | translation, and keeping the code fast at the same time, the constructs of the |
| 1396 | tool need to be supported. Since most languages support classes the lack of |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1397 | support for classes in Vim is then a problem. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1398 | |
Bram Moolenaar | 1d59aa1 | 2020-09-19 18:50:13 +0200 | [diff] [blame] | 1399 | |
| 1400 | Classes ~ |
| 1401 | |
| 1402 | Vim supports a kind-of object oriented programming by adding methods to a |
| 1403 | dictionary. With some care this can be made to work, but it does not look |
| 1404 | like real classes. On top of that, it's quite slow, because of the use of |
| 1405 | dictionaries. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1406 | |
| 1407 | The support of classes in Vim9 script is a "minimal common functionality" of |
Bram Moolenaar | 1c6737b | 2020-09-07 22:18:52 +0200 | [diff] [blame] | 1408 | class support in most languages. It works much like Java, which is the most |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1409 | popular programming language. |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | vim:tw=78:ts=8:noet:ft=help:norl: |