Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 1 | *vim9.txt* For Vim version 8.2. Last change: 2020 Aug 01 |
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 | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 9 | Vim9 script commands and expressions. *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 | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [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| |
| 22 | |
| 23 | 9. Rationale |vim9-rationale| |
| 24 | |
| 25 | ============================================================================== |
| 26 | |
| 27 | 1. What is Vim9 script? *vim9-script* |
| 28 | |
| 29 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 30 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 31 | Vim script has been growing over time, while preserving backwards |
| 32 | 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] | 33 | and compatibility with Vi restricts possible solutions. Execution is quite |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 34 | slow, each line is parsed every time it is executed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 35 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 36 | The main goal of Vim9 script is to drastically improve performance. This is |
| 37 | accomplished by compiling commands into instructions that can be efficiently |
| 38 | executed. An increase in execution speed of 10 to 100 times can be expected. |
| 39 | |
| 40 | A secondary goal is to avoid Vim-specific constructs and get closer to |
| 41 | commonly used programming languages, such as JavaScript, TypeScript and Java. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 42 | |
| 43 | The performance improvements can only be achieved by not being 100% backwards |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 44 | compatible. For example, making function arguments available in the |
| 45 | "a:" dictionary adds quite a lot of overhead. In a Vim9 function this |
| 46 | dictionary is not available. Other differences are more subtle, such as how |
| 47 | errors are handled. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | |
| 49 | The Vim9 script syntax and semantics are used in: |
| 50 | - a function defined with the `:def` command |
| 51 | - a script file where the first command is `vim9script` |
| 52 | |
| 53 | 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] | 54 | However, this can be confusing and is therefore discouraged. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 55 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 56 | Vim9 script and legacy Vim script can be mixed. There is no requirement to |
| 57 | rewrite old scripts, they keep working as before. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 58 | |
| 59 | ============================================================================== |
| 60 | |
| 61 | 2. Differences from legacy Vim script *vim9-differences* |
| 62 | |
| 63 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 64 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 65 | Comments starting with # ~ |
| 66 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 67 | In legacy Vim script comments start with double quote. In Vim9 script |
| 68 | comments start with #. > |
| 69 | # declarations |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 70 | let count = 0 # number of occurrences |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 71 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 72 | The reason is that a double quote can also be the start of a string. In many |
| 73 | places, especially halfway an expression with a line break, it's hard to tell |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 74 | what the meaning is, since both a string and a comment can be followed by |
| 75 | arbitrary text. To avoid confusion only # comments are recognized. This is |
| 76 | the same as in shell scripts and Python programs. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 77 | |
| 78 | In Vi # is a command to list text with numbers. In Vim9 script you can use |
| 79 | `:number` for that. > |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 80 | 101 number |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 81 | |
| 82 | To improve readability there must be a space between a command and the # |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 83 | that starts a comment. Note that #{ is the start of a dictionary, therefore |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 84 | it does not start a comment. |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 85 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 86 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 87 | Vim9 functions ~ |
| 88 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 89 | A function defined with `:def` is compiled. Execution is many times faster, |
| 90 | often 10x to 100x times. |
| 91 | |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 92 | Many errors are already found when compiling, before the function is executed. |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 93 | The syntax is strict, to enforce code that is easy to read and understand. |
| 94 | |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 95 | Compilation is done when the function is first called, or when the |
| 96 | `:defcompile` command is encountered in the script where the function was |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 97 | defined. `:disassemble` also compiles the function. |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 98 | |
| 99 | `:def` has no options like `:function` does: "range", "abort", "dict" or |
| 100 | "closure". A `:def` function always aborts on an error, does not get a range |
| 101 | passed and cannot be a "dict" function. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 103 | The argument types and return type need to be specified. The "any" type can |
| 104 | be used, type checking will then be done at runtime, like with legacy |
| 105 | functions. |
| 106 | |
| 107 | Arguments are accessed by name, without "a:". There is no "a:" dictionary or |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 108 | "a:000" list. Just like any other language. |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 109 | |
| 110 | Variable arguments are defined as the last argument, with a name and have a |
| 111 | list type, similar to Typescript. For example, a list of numbers: > |
| 112 | def MyFunc(...itemlist: list<number>) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 113 | for item in itemlist |
| 114 | ... |
| 115 | |
| 116 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 117 | Functions and variables are script-local by default ~ |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 118 | *vim9-scopes* |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 119 | When using `:function` or `:def` to specify a new function at the script level |
| 120 | in a Vim9 script, the function is local to the script, as if "s:" was |
Bram Moolenaar | ea2d8d2 | 2020-07-29 22:11:05 +0200 | [diff] [blame] | 121 | prefixed. Using the "s:" prefix is optional. To define or use a global |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 122 | function or variable the "g:" prefix should be used. For functions in an |
Bram Moolenaar | ea2d8d2 | 2020-07-29 22:11:05 +0200 | [diff] [blame] | 123 | autoload script the "name#" prefix is sufficient. > |
| 124 | def ThisFunction() # script-local |
| 125 | def s:ThisFunction() # script-local |
| 126 | def g:ThatFunction() # global |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 127 | def ThatFunction() # global if no local ThatFunction() |
Bram Moolenaar | ea2d8d2 | 2020-07-29 22:11:05 +0200 | [diff] [blame] | 128 | def scriptname#function() # autoload |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 129 | |
| 130 | When using `:function` or `:def` to specify a new function inside a function, |
| 131 | the function is local to the function. It is not possible to define a |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 132 | script-local function inside a function. It is possible to define a global |
| 133 | function, using the "g:" prefix. |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 134 | |
| 135 | When referring to a function and no "s:" or "g:" prefix is used, Vim will |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 136 | prefer using a local function (in the function scope, script scope or |
| 137 | imported) before looking for a global function. |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 138 | In all cases the function must be defined before used. That is when it is |
| 139 | first called or when `:defcompile` causes the call to be compiled. |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 140 | |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 141 | 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] | 142 | found in the script, either defined there or imported. Global functions and |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 143 | variables could be defined anywhere (good luck finding out where!). |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 144 | |
Bram Moolenaar | 2cfb4a2 | 2020-05-07 18:56:00 +0200 | [diff] [blame] | 145 | Global functions can be still be defined and deleted at nearly any time. In |
| 146 | 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] | 147 | and cannot be deleted or replaced. |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 148 | |
| 149 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 150 | Variable declarations with :let and :const ~ |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 151 | *vim9-declaration* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 152 | Local variables need to be declared with `:let`. Local constants need to be |
| 153 | declared with `:const`. We refer to both as "variables". |
| 154 | |
| 155 | Variables can be local to a script, function or code block: > |
| 156 | vim9script |
| 157 | let script_var = 123 |
| 158 | def SomeFunc() |
| 159 | let func_var = script_var |
| 160 | if cond |
| 161 | let block_var = func_var |
| 162 | ... |
| 163 | |
| 164 | The variables are only visible in the block where they are defined and nested |
| 165 | blocks. Once the block ends the variable is no longer accessible: > |
| 166 | if cond |
| 167 | let inner = 5 |
| 168 | else |
| 169 | let inner = 0 |
| 170 | endif |
| 171 | echo inner " Error! |
| 172 | |
| 173 | The declaration must be done earlier: > |
| 174 | let inner: number |
| 175 | if cond |
| 176 | inner = 5 |
| 177 | else |
| 178 | inner = 0 |
| 179 | endif |
| 180 | echo inner |
| 181 | |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 182 | To intentionally avoid a variable being available later, a block can be used: |
| 183 | > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 184 | { |
| 185 | let temp = 'temp' |
| 186 | ... |
| 187 | } |
| 188 | echo temp " Error! |
| 189 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 190 | An existing variable cannot be assigned to with `:let`, since that implies a |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 191 | declaration. Global, window, tab, buffer and Vim variables can only be used |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 192 | without `:let`, because they are not really declared, they can also be deleted |
| 193 | with `:unlet`. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 194 | |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 195 | Variables and functions cannot shadow previously defined or imported variables |
| 196 | and functions. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 197 | Variables may shadow Ex commands, rename the variable if needed. |
| 198 | |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 199 | Global variables and user defined functions must be prefixed with "g:", also |
| 200 | at the script level. > |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 201 | vim9script |
| 202 | let script_local = 'text' |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 203 | g:global = 'value' |
Bram Moolenaar | 7ceefb3 | 2020-05-01 16:07:38 +0200 | [diff] [blame] | 204 | let Funcref = g:ThatFunction |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 205 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 206 | Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be |
| 207 | used to repeat a `:substitute` command. |
| 208 | |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 209 | *E1092* |
| 210 | Declaring more than one variable at a time, using the unpack notation, is |
| 211 | currently not supported: > |
| 212 | let [v1, v2] = GetValues() # Error! |
| 213 | That is because the type needs to be inferred from the list item type, which |
| 214 | isn't that easy. |
| 215 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 216 | |
| 217 | Omitting :call and :eval ~ |
| 218 | |
| 219 | Functions can be called without `:call`: > |
| 220 | writefile(lines, 'file') |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 221 | Using `:call` is still possible, but this is discouraged. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 222 | |
| 223 | 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] | 224 | identifier or can't be an Ex command. Examples: > |
| 225 | myList->add(123) |
| 226 | g:myList->add(123) |
| 227 | [1, 2, 3]->Process() |
| 228 | #{a: 1, b: 2}->Process() |
| 229 | {'a': 1, 'b': 2}->Process() |
| 230 | "foobar"->Process() |
| 231 | ("foobar")->Process() |
| 232 | 'foobar'->Process() |
| 233 | ('foobar')->Process() |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 234 | |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 235 | In rare case there is ambiguity between a function name and an Ex command, |
| 236 | prepend ":" to make clear you want to use the Ex command. For example, there |
| 237 | is both the `:substitute` command and the `substitute()` function. When the |
| 238 | line starts with `substitute(` this will use the function. Prepend a colon to |
| 239 | use the command instead: > |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 240 | :substitute(pattern (replacement ( |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 241 | |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 242 | Note that while variables need to be defined before they can be used, |
| 243 | functions can be called before being defined. This is required to be able |
| 244 | have cyclic dependencies between functions. It is slightly less efficient, |
| 245 | 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] | 246 | name will only be found when the function is called. |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 247 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 248 | |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 249 | Omitting function() ~ |
| 250 | |
| 251 | A user defined function can be used as a function reference in an expression |
| 252 | without `function()`. The argument types and return type will then be checked. |
| 253 | The function must already have been defined. > |
| 254 | |
| 255 | let Funcref = MyFunction |
| 256 | |
| 257 | When using `function()` the resulting type is "func", a function with any |
| 258 | number of arguments and any return type. The function can be defined later. |
| 259 | |
| 260 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 261 | Automatic line continuation ~ |
| 262 | |
| 263 | In many cases it is obvious that an expression continues on the next line. In |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 264 | those cases there is no need to prefix the line with a backslash |
| 265 | |line-continuation|. For example, when a list spans multiple lines: > |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 266 | let mylist = [ |
| 267 | 'one', |
| 268 | 'two', |
| 269 | ] |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 270 | And when a dict spans multiple lines: > |
| 271 | let mydict = #{ |
| 272 | one: 1, |
| 273 | two: 2, |
| 274 | } |
| 275 | Function call: > |
| 276 | let result = Func( |
| 277 | arg1, |
| 278 | arg2 |
| 279 | ) |
| 280 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 281 | For binary operators in expressions not in [], {} or () a line break is |
| 282 | possible just before or after the operator. For example: > |
| 283 | let text = lead |
| 284 | .. middle |
| 285 | .. end |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 286 | let total = start + |
| 287 | end - |
| 288 | correction |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 289 | let result = positive |
| 290 | ? PosFunc(arg) |
| 291 | : NegFunc(arg) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 292 | |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 293 | For a method call using "->" and a member using a dot, a line break is allowed |
| 294 | before it: > |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 295 | let result = GetBuilder() |
| 296 | ->BuilderSetWidth(333) |
| 297 | ->BuilderSetHeight(777) |
| 298 | ->BuilderBuild() |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 299 | let result = MyDict |
| 300 | .member |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 301 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 302 | < *E1050* |
| 303 | 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] | 304 | 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] | 305 | "start" and print: > |
| 306 | let result = start |
| 307 | + print |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 308 | Like this: > |
| 309 | let result = start + print |
| 310 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 311 | This will assign "start" and print a line: > |
| 312 | let result = start |
| 313 | :+ print |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 314 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 315 | It is also possible to split a function header over multiple lines, in between |
| 316 | arguments: > |
| 317 | def MyFunc( |
| 318 | text: string, |
| 319 | separator = '-' |
| 320 | ): string |
| 321 | |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 322 | Notes: |
| 323 | - "enddef" cannot be used at the start of a continuation line, it ends the |
| 324 | current function. |
| 325 | - No line break is allowed in the LHS of an assignment. Specifically when |
| 326 | unpacking a list |:let-unpack|. This is OK: > |
| 327 | [var1, var2] = |
| 328 | Func() |
| 329 | < This does not work: > |
| 330 | [var1, |
| 331 | var2] = |
| 332 | Func() |
| 333 | - No line break is allowed in between arguments of an `:echo`, `:execute` and |
| 334 | similar commands. This is OK: > |
| 335 | echo [1, |
| 336 | 2] [3, |
| 337 | 4] |
| 338 | < This does not work: > |
| 339 | echo [1, 2] |
| 340 | [3, 4] |
| 341 | - No line break is allowed in the arguments of a lambda, between the "{" and |
| 342 | "->". This is OK: > |
| 343 | filter(list, {k, v -> |
| 344 | v > 0}) |
| 345 | < This does not work: > |
| 346 | filter(list, {k, |
| 347 | v -> v > 0}) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 348 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 349 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 350 | No curly braces expansion ~ |
| 351 | |
| 352 | |curly-braces-names| cannot be used. |
| 353 | |
| 354 | |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 355 | No :xit, :t, :append, :change or :insert ~ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 356 | |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 357 | These commands are too easily confused with local variable names. |
| 358 | Instead of `:x` or `:xit` you can use `:exit`. |
| 359 | Instead of `:t` you can use `:copy`. |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 360 | |
| 361 | |
| 362 | Comparators ~ |
| 363 | |
| 364 | The 'ignorecase' option is not used for comparators that use strings. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 365 | |
| 366 | |
| 367 | White space ~ |
| 368 | |
| 369 | Vim9 script enforces proper use of white space. This is no longer allowed: > |
| 370 | let var=234 " Error! |
| 371 | let var= 234 " Error! |
| 372 | let var =234 " Error! |
| 373 | There must be white space before and after the "=": > |
| 374 | let var = 234 " OK |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 375 | White space must also be put before the # that starts a comment after a |
| 376 | command: > |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 377 | let var = 234# Error! |
| 378 | let var = 234 # OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 379 | |
| 380 | White space is required around most operators. |
| 381 | |
| 382 | White space is not allowed: |
| 383 | - Between a function name and the "(": > |
| 384 | call Func (arg) " Error! |
| 385 | call Func |
| 386 | \ (arg) " Error! |
| 387 | call Func(arg) " OK |
| 388 | call Func( |
| 389 | \ arg) " OK |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 390 | call Func( |
| 391 | \ arg " OK |
| 392 | \ ) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 393 | |
| 394 | |
| 395 | Conditions and expressions ~ |
| 396 | |
| 397 | Conditions and expression are mostly working like they do in JavaScript. A |
| 398 | difference is made where JavaScript does not work like most people expect. |
| 399 | Specifically, an empty list is falsey. |
| 400 | |
| 401 | Any type of variable can be used as a condition, there is no error, not even |
| 402 | for using a list or job. This is very much like JavaScript, but there are a |
| 403 | few exceptions. |
| 404 | |
| 405 | type TRUE when ~ |
| 406 | bool v:true |
| 407 | number non-zero |
| 408 | float non-zero |
| 409 | string non-empty |
| 410 | blob non-empty |
| 411 | list non-empty (different from JavaScript) |
| 412 | dictionary non-empty (different from JavaScript) |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 413 | func when there is a function name |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 414 | special v:true |
| 415 | job when not NULL |
| 416 | channel when not NULL |
| 417 | class when not NULL |
| 418 | object when not NULL (TODO: when isTrue() returns v:true) |
| 419 | |
| 420 | The boolean operators "||" and "&&" do not change the value: > |
| 421 | 8 || 2 == 8 |
| 422 | 0 || 2 == 2 |
| 423 | 0 || '' == '' |
| 424 | 8 && 2 == 2 |
| 425 | 0 && 2 == 0 |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 426 | 2 && 0 == 0 |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 427 | [] && 2 == [] |
| 428 | |
| 429 | When using `..` for string concatenation the arguments are always converted to |
| 430 | string. > |
| 431 | 'hello ' .. 123 == 'hello 123' |
| 432 | 'hello ' .. v:true == 'hello true' |
| 433 | |
| 434 | In Vim9 script one can use "true" for v:true and "false" for v:false. |
| 435 | |
| 436 | |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 437 | What to watch out for ~ |
| 438 | *vim9-gotchas* |
| 439 | Vim9 was designed to be closer to often used programming languages, but at the |
| 440 | same time tries to support the legacy Vim commands. Some compromises had to |
| 441 | be made. Here is a summary of what might be unexpected. |
| 442 | |
| 443 | Ex command ranges need to be prefixed with a colon. > |
| 444 | -> " legacy Vim: shifts the previous line to the right |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 445 | ->func() " Vim9: method call in continuation line |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 446 | :-> " Vim9: shifts the previous line to the right |
| 447 | |
| 448 | %s/a/b " legacy Vim: substitute on all lines |
| 449 | x = alongname |
| 450 | % another " Vim9: line continuation without a backslash |
| 451 | :%s/a/b " Vim9: substitute on all lines |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 452 | 'text'->func() " Vim9: method call |
| 453 | :'t " legacy Vim: jump to mark m |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 454 | |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 455 | Some Ex commands can be confused with assignments in Vim9 script: > |
| 456 | g:name = value # assignment |
| 457 | g:pattern:cmd # invalid command - ERROR |
| 458 | :g:pattern:cmd # :global command |
| 459 | |
Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 460 | Functions defined with `:def` compile the whole function. Legacy functions |
| 461 | can bail out, and the following lines are not parsed: > |
| 462 | func Maybe() |
| 463 | if !has('feature') |
| 464 | return |
| 465 | endif |
| 466 | use-feature |
| 467 | endfunc |
| 468 | Vim9 functions are compiled as a whole: > |
| 469 | def Maybe() |
| 470 | if !has('feature') |
| 471 | return |
| 472 | endif |
| 473 | use-feature " May give compilation error |
| 474 | enddef |
| 475 | For a workaround, split it in two functions: > |
| 476 | func Maybe() |
| 477 | if has('feature') |
| 478 | call MaybyInner() |
| 479 | endif |
| 480 | endfunc |
| 481 | if has('feature') |
| 482 | def MaybeInner() |
| 483 | use-feature |
| 484 | enddef |
| 485 | endif |
| 486 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 487 | ============================================================================== |
| 488 | |
| 489 | 3. New style functions *fast-functions* |
| 490 | |
| 491 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 492 | |
| 493 | *:def* |
| 494 | :def[!] {name}([arguments])[: {return-type} |
| 495 | Define a new function by the name {name}. The body of |
| 496 | the function follows in the next lines, until the |
| 497 | matching `:enddef`. |
| 498 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 499 | When {return-type} is omitted or is "void" the |
| 500 | function is not expected to return anything. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 501 | |
| 502 | {arguments} is a sequence of zero or more argument |
| 503 | declarations. There are three forms: |
| 504 | {name}: {type} |
| 505 | {name} = {value} |
| 506 | {name}: {type} = {value} |
| 507 | The first form is a mandatory argument, the caller |
| 508 | must always provide them. |
| 509 | The second and third form are optional arguments. |
| 510 | When the caller omits an argument the {value} is used. |
| 511 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 512 | The function will be compiled into instructions when |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 513 | called, or when `:disassemble` or `:defcompile` is |
| 514 | used. Syntax and type errors will be produced at that |
| 515 | time. |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 516 | |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 517 | It is possible to nest `:def` inside another `:def` or |
| 518 | `:function` up to about 50 levels deep. |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 519 | |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 520 | [!] is used as with `:function`. Note that in Vim9 |
| 521 | script script-local functions cannot be deleted or |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 522 | redefined later in the same script. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 523 | |
| 524 | *:enddef* |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 525 | :enddef End of a function defined with `:def`. It should be on |
| 526 | a line by its own. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 527 | |
| 528 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 529 | If the script the function is defined in is Vim9 script, then script-local |
| 530 | variables can be accessed without the "s:" prefix. They must be defined |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 531 | before the function is compiled. If the script the function is defined in is |
| 532 | legacy script, then script-local variables must be accessed with the "s:" |
| 533 | prefix. |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 534 | |
Bram Moolenaar | 388a5d4 | 2020-05-26 21:20:45 +0200 | [diff] [blame] | 535 | *:defc* *:defcompile* |
| 536 | :defc[ompile] Compile functions defined in the current script that |
| 537 | were not compiled yet. |
| 538 | This will report errors found during the compilation. |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 539 | |
Bram Moolenaar | ebdf3c9 | 2020-02-15 21:41:42 +0100 | [diff] [blame] | 540 | *:disa* *:disassemble* |
| 541 | :disa[ssemble] {func} Show the instructions generated for {func}. |
| 542 | This is for debugging and testing. |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 543 | Note that for command line completion of {func} you |
| 544 | can prepend "s:" to find script-local functions. |
Bram Moolenaar | ebdf3c9 | 2020-02-15 21:41:42 +0100 | [diff] [blame] | 545 | |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 546 | Limitations ~ |
| 547 | |
| 548 | Local variables will not be visible to string evaluation. For example: > |
| 549 | def EvalString(): list<string> |
| 550 | let list = ['aa', 'bb', 'cc', 'dd'] |
| 551 | return range(1, 2)->map('list[v:val]') |
| 552 | enddef |
| 553 | |
| 554 | The map argument is a string expression, which is evaluated without the |
| 555 | function scope. Instead, use a lambda: > |
| 556 | def EvalString(): list<string> |
| 557 | let list = ['aa', 'bb', 'cc', 'dd'] |
| 558 | return range(1, 2)->map({ _, v -> list[v] }) |
| 559 | enddef |
| 560 | |
| 561 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 562 | ============================================================================== |
| 563 | |
| 564 | 4. Types *vim9-types* |
| 565 | |
| 566 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 567 | |
| 568 | The following builtin types are supported: |
| 569 | bool |
| 570 | number |
| 571 | float |
| 572 | string |
| 573 | blob |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 574 | list<{type}> |
| 575 | dict<{type}> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 576 | job |
| 577 | channel |
Bram Moolenaar | b17893a | 2020-03-14 08:19:51 +0100 | [diff] [blame] | 578 | func |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 579 | func: {type} |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 580 | func({type}, ...) |
| 581 | func({type}, ...): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 582 | |
| 583 | Not supported yet: |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 584 | tuple<a: {type}, b: {type}, ...> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 585 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 586 | 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] | 587 | {type}|{type} {not implemented yet} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 588 | void |
| 589 | any |
| 590 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 591 | 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] | 592 | efficient implementation is used that avoids allocating lot of small pieces of |
| 593 | memory. |
| 594 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 595 | A partial and function can be declared in more or less specific ways: |
| 596 | func any kind of function reference, no type |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 597 | checking for arguments or return value |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 598 | func: {type} any number and type of arguments with specific |
| 599 | return type |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 600 | func({type}) function with argument type, does not return |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 601 | a value |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 602 | func({type}): {type} function with argument type and return type |
| 603 | func(?{type}) function with type of optional argument, does |
| 604 | not return a value |
| 605 | func(...{type}) function with type of variable number of |
| 606 | arguments, does not return a value |
| 607 | func({type}, ?{type}, ...{type}): {type} |
| 608 | function with: |
| 609 | - type of mandatory argument |
| 610 | - type of optional argument |
| 611 | - type of variable number of arguments |
| 612 | - return type |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 613 | |
| 614 | If the return type is "void" the function does not return a value. |
| 615 | |
| 616 | The reference can also be a |Partial|, in which case it stores extra arguments |
| 617 | and/or a dictionary, which are not visible to the caller. Since they are |
| 618 | called in the same way the declaration is the same. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 619 | |
| 620 | Custom types can be defined with `:type`: > |
| 621 | :type MyList list<string> |
| 622 | {not implemented yet} |
| 623 | |
| 624 | And classes and interfaces can be used as types: > |
| 625 | :class MyClass |
| 626 | :let mine: MyClass |
| 627 | |
| 628 | :interface MyInterface |
| 629 | :let mine: MyInterface |
| 630 | |
| 631 | :class MyTemplate<Targ> |
| 632 | :let mine: MyTemplate<number> |
| 633 | :let mine: MyTemplate<string> |
| 634 | |
| 635 | :class MyInterface<Targ> |
| 636 | :let mine: MyInterface<number> |
| 637 | :let mine: MyInterface<string> |
| 638 | {not implemented yet} |
| 639 | |
| 640 | |
| 641 | Type inference *type-inference* |
| 642 | |
| 643 | In general: Whenever the type is clear it can be omitted. For example, when |
| 644 | declaring a variable and giving it a value: > |
| 645 | let var = 0 " infers number type |
| 646 | let var = 'hello' " infers string type |
| 647 | |
| 648 | |
| 649 | ============================================================================== |
| 650 | |
| 651 | 5. Namespace, Import and Export |
| 652 | *vim9script* *vim9-export* *vim9-import* |
| 653 | |
| 654 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 655 | |
| 656 | A Vim9 script can be written to be imported. This means that everything in |
| 657 | the script is local, unless exported. Those exported items, and only those |
| 658 | items, can then be imported in another script. |
| 659 | |
| 660 | |
| 661 | Namespace ~ |
| 662 | *:vim9script* *:vim9* |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 663 | To recognize a file that can be imported the `vim9script` statement must |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 664 | appear as the first statement in the file. It tells Vim to interpret the |
| 665 | script in its own namespace, instead of the global namespace. If a file |
| 666 | starts with: > |
| 667 | vim9script |
| 668 | let myvar = 'yes' |
| 669 | Then "myvar" will only exist in this file. While without `vim9script` it would |
| 670 | be available as `g:myvar` from any other script and function. |
| 671 | |
| 672 | 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] | 673 | variables in legacy Vim script, but the "s:" is omitted. And they cannot be |
| 674 | deleted. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 675 | |
Bram Moolenaar | 2c7f8c5 | 2020-04-20 19:52:53 +0200 | [diff] [blame] | 676 | In Vim9 script the global "g:" namespace can still be used as before. And the |
| 677 | "w:", "b:" and "t:" namespaces. These have in common that variables are not |
| 678 | declared and they can be deleted. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 679 | |
| 680 | A side effect of `:vim9script` is that the 'cpoptions' option is set to the |
| 681 | Vim default value, like with: > |
| 682 | :set cpo&vim |
| 683 | One of the effects is that |line-continuation| is always enabled. |
| 684 | The original value of 'cpoptions' is restored at the end of the script. |
| 685 | |
| 686 | |
| 687 | Export ~ |
| 688 | *:export* *:exp* |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 689 | Exporting an item can be written as: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 690 | export const EXPORTED_CONST = 1234 |
| 691 | export let someValue = ... |
| 692 | export def MyFunc() ... |
| 693 | export class MyClass ... |
| 694 | |
| 695 | As this suggests, only constants, variables, `:def` functions and classes can |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 696 | be exported. {classes are not implemented yet} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 697 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 698 | *E1042* |
| 699 | `:export` can only be used in Vim9 script, at the script level. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 700 | |
| 701 | |
| 702 | Import ~ |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 703 | *:import* *:imp* *E1094* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 704 | The exported items can be imported individually in another Vim9 script: > |
| 705 | import EXPORTED_CONST from "thatscript.vim" |
| 706 | import MyClass from "myclass.vim" |
| 707 | |
| 708 | To import multiple items at the same time: > |
| 709 | import {someValue, MyClass} from "thatscript.vim" |
| 710 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 711 | In case the name is ambiguous, another name can be specified: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 712 | import MyClass as ThatClass from "myclass.vim" |
| 713 | import {someValue, MyClass as ThatClass} from "myclass.vim" |
| 714 | |
| 715 | To import all exported items under a specific identifier: > |
| 716 | import * as That from 'thatscript.vim' |
| 717 | |
| 718 | Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free |
| 719 | to choose the name "That", but it is highly recommended to use the name of the |
| 720 | script file to avoid confusion. |
| 721 | |
| 722 | The script name after `import` can be: |
| 723 | - A relative path, starting "." or "..". This finds a file relative to the |
| 724 | location of the script file itself. This is useful to split up a large |
| 725 | plugin into several files. |
| 726 | - An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This |
| 727 | will be rarely used. |
| 728 | - A path not being relative or absolute. This will be found in the |
| 729 | "import" subdirectories of 'runtimepath' entries. The name will usually be |
| 730 | longer and unique, to avoid loading the wrong file. |
| 731 | |
| 732 | Once a vim9 script file has been imported, the result is cached and used the |
| 733 | next time the same script is imported. It will not be read again. |
| 734 | *:import-cycle* |
| 735 | The `import` commands are executed when encountered. If that script (directly |
| 736 | or indirectly) imports the current script, then items defined after the |
| 737 | `import` won't be processed yet. Therefore cyclic imports can exist, but may |
| 738 | result in undefined items. |
| 739 | |
| 740 | |
| 741 | Import in an autoload script ~ |
| 742 | |
| 743 | For optimal startup speed, loading scripts should be postponed until they are |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 744 | actually needed. A recommended mechanism: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 745 | |
| 746 | 1. In the plugin define user commands, functions and/or mappings that refer to |
| 747 | an autoload script. > |
| 748 | command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>) |
| 749 | |
| 750 | < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. |
| 751 | |
| 752 | 2. In the autocommand script do the actual work. You can import items from |
| 753 | other files to split up functionality in appropriate pieces. > |
| 754 | vim9script |
| 755 | import FilterFunc from "../import/someother.vim" |
| 756 | def searchfor#Stuff(arg: string) |
| 757 | let filtered = FilterFunc(arg) |
| 758 | ... |
| 759 | < This goes in .../autoload/searchfor.vim. "searchfor" in the file name |
| 760 | must be exactly the same as the prefix for the function name, that is how |
| 761 | Vim finds the file. |
| 762 | |
| 763 | 3. Other functionality, possibly shared between plugins, contains the exported |
| 764 | items and any private items. > |
| 765 | vim9script |
| 766 | let localVar = 'local' |
| 767 | export def FilterFunc(arg: string): string |
| 768 | ... |
| 769 | < This goes in .../import/someother.vim. |
| 770 | |
| 771 | |
| 772 | Import in legacy Vim script ~ |
| 773 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 774 | If an `import` statement is used in legacy Vim script, the script-local "s:" |
| 775 | 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] | 776 | |
| 777 | |
| 778 | ============================================================================== |
| 779 | |
| 780 | 9. Rationale *vim9-rationale* |
| 781 | |
| 782 | The :def command ~ |
| 783 | |
| 784 | Plugin writers have asked for a much faster Vim script. Investigation have |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 785 | 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] | 786 | impossible, because of the overhead involved with calling a function, setting |
| 787 | up the local function scope and executing lines. There are many details that |
| 788 | need to be handled, such as error messages and exceptions. The need to create |
| 789 | a dictionary for a: and l: scopes, the a:000 list and several others add too |
| 790 | much overhead that cannot be avoided. |
| 791 | |
| 792 | Therefore the `:def` method to define a new-style function had to be added, |
| 793 | which allows for a function with different semantics. Most things still work |
| 794 | as before, but some parts do not. A new way to define a function was |
| 795 | considered the best way to separate the old-style code from Vim9 script code. |
| 796 | |
| 797 | Using "def" to define a function comes from Python. Other languages use |
| 798 | "function" which clashes with legacy Vim script. |
| 799 | |
| 800 | |
| 801 | Type checking ~ |
| 802 | |
| 803 | When compiling lines of Vim commands into instructions as much as possible |
| 804 | should be done at compile time. Postponing it to runtime makes the execution |
| 805 | slower and means mistakes are found only later. For example, when |
| 806 | encountering the "+" character and compiling this into a generic add |
| 807 | instruction, at execution time the instruction would have to inspect the type |
| 808 | of the arguments and decide what kind of addition to do. And when the |
| 809 | type is dictionary throw an error. If the types are known to be numbers then |
| 810 | an "add number" instruction can be used, which is faster. The error can be |
| 811 | given at compile time, no error handling is needed at runtime. |
| 812 | |
| 813 | The syntax for types is similar to Java, since it is easy to understand and |
| 814 | widely used. The type names are what was used in Vim before, with some |
| 815 | additions such as "void" and "bool". |
| 816 | |
| 817 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 818 | Compiling functions early ~ |
| 819 | |
| 820 | Functions are compiled when called or when `:defcompile` is used. Why not |
| 821 | compile them early, so that syntax and type errors are reported early? |
| 822 | |
| 823 | The functions can't be compiled right away when encountered, because there may |
| 824 | be forward references to functions defined later. Consider defining functions |
| 825 | A, B and C, where A calls B, B calls C, and C calls A again. It's impossible |
| 826 | to reorder the functions to avoid forward references. |
| 827 | |
| 828 | An alternative would be to first scan through the file to locate items and |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 829 | figure out their type, so that forward references are found, and only then |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 830 | execute the script and compile the functions. This means the script has to be |
| 831 | parsed twice, which is slower, and some conditions at the script level, such |
| 832 | as checking if a feature is supported, are hard to use. An attempt was made |
| 833 | to see if it works, but it turned out to be impossible to make work nicely. |
| 834 | |
| 835 | It would be possible to compile all the functions at the end of the script. |
| 836 | The drawback is that if a function never gets called, the overhead of |
| 837 | compiling it counts anyway. Since startup speed is very important, in most |
| 838 | cases it's better to do it later and accept that syntax and type errors are |
| 839 | only reported then. In case these errors should be found early, e.g. when |
| 840 | testing, the `:defcompile` command will help out. |
| 841 | |
| 842 | |
| 843 | TypeScript syntax and semantics ~ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 844 | |
| 845 | Script writers have complained that the Vim script syntax is unexpectedly |
| 846 | different from what they are used to. To reduce this complaint popular |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 847 | languages are used as an example. At the same time, we do not want to abandon |
| 848 | the well-known parts of legacy Vim script. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 849 | |
| 850 | Since Vim already uses `:let` and `:const` and optional type checking is |
| 851 | desirable, the JavaScript/TypeScript syntax fits best for variable |
| 852 | declarations. > |
| 853 | const greeting = 'hello' " string type is inferred |
| 854 | let name: string |
| 855 | ... |
| 856 | name = 'John' |
| 857 | |
| 858 | Expression evaluation was already close to what JavaScript and other languages |
| 859 | are doing. Some details are unexpected and can be fixed. For example how the |
| 860 | || and && operators work. Legacy Vim script: > |
| 861 | let result = 44 |
| 862 | ... |
| 863 | return result || 0 " returns 1 |
| 864 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 865 | Vim9 script works like JavaScript/Typescript, keep the value: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 866 | let result = 44 |
| 867 | ... |
| 868 | return result || 0 " returns 44 |
| 869 | |
| 870 | On the other hand, overloading "+" to use both for addition and string |
| 871 | concatenation goes against legacy Vim script and often leads to mistakes. |
| 872 | For that reason we will keep using ".." for string concatenation. Lua also |
| 873 | uses ".." this way. |
| 874 | |
| 875 | |
| 876 | Import and Export ~ |
| 877 | |
| 878 | A problem of legacy Vim script is that by default all functions and variables |
| 879 | are global. It is possible to make them script-local, but then they are not |
| 880 | available in other scripts. |
| 881 | |
| 882 | In Vim9 script a mechanism very similar to the Javascript import and export |
| 883 | mechanism is supported. It is a variant to the existing `:source` command |
| 884 | that works like one would expect: |
| 885 | - Instead of making everything global by default, everything is script-local, |
| 886 | unless exported. |
| 887 | - When importing a script the symbols that are imported are listed, avoiding |
| 888 | name conflicts and failures if later functionality is added. |
| 889 | - The mechanism allows for writing a big, long script with a very clear API: |
| 890 | the exported function(s) and class(es). |
| 891 | - By using relative paths loading can be much faster for an import inside of a |
| 892 | package, no need to search many directories. |
| 893 | - Once an import has been used, it can be cached and loading it again can be |
| 894 | avoided. |
| 895 | - The Vim-specific use of "s:" to make things script-local can be dropped. |
| 896 | |
Bram Moolenaar | 65e0d77 | 2020-06-14 17:29:55 +0200 | [diff] [blame] | 897 | When sourcing a Vim9 script from a legacy script, only the items defined |
| 898 | globally can be used, not the exported items. Alternatives considered: |
| 899 | - All the exported items become available as script-local items. This makes |
| 900 | it uncontrollable what items get defined. |
| 901 | - Use the exported items and make them global. Disadvantage is that it's then |
| 902 | not possible to avoid name clashes in the global namespace. |
| 903 | - Completely disallow sourcing a Vim9 script, require using `:import`. That |
| 904 | makes it difficult to use scripts for testing, or sourcing them from the |
| 905 | command line to try them out. |
| 906 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 907 | |
| 908 | Classes ~ |
| 909 | |
| 910 | Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But |
| 911 | these have never become widespread. When Vim 9 was designed a decision was |
| 912 | made to phase out these interfaces and concentrate on Vim script, while |
| 913 | encouraging plugin authors to write code in any language and run it as an |
| 914 | external tool, using jobs and channels. |
| 915 | |
| 916 | Still, using an external tool has disadvantages. An alternative is to convert |
| 917 | the tool into Vim script. For that to be possible without too much |
| 918 | translation, and keeping the code fast at the same time, the constructs of the |
| 919 | tool need to be supported. Since most languages support classes the lack of |
| 920 | class support in Vim is then a problem. |
| 921 | |
| 922 | Previously Vim supported a kind-of object oriented programming by adding |
| 923 | methods to a dictionary. With some care this could be made to work, but it |
| 924 | does not look like real classes. On top of that, it's very slow, because of |
| 925 | the use of dictionaries. |
| 926 | |
| 927 | The support of classes in Vim9 script is a "minimal common functionality" of |
| 928 | class support in most languages. It works mostly like Java, which is the most |
| 929 | popular programming language. |
| 930 | |
| 931 | |
| 932 | |
| 933 | vim:tw=78:ts=8:noet:ft=help:norl: |