Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 1 | *vim9.txt* For Vim version 8.2. Last change: 2020 Apr 09 |
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 | |
| 9 | Vim9 script commands and expressions. |
| 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 | |
| 17 | 1 What is Vim9 script? |vim9-script| |
| 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 | |
| 31 | Vim script has been growing over time, while keeping backwards compatibility. |
| 32 | That means bad choices from the past often can't be changed. Execution is |
| 33 | quite slow, every line is parsed every time it is executed. |
| 34 | |
| 35 | The main goal of Vim9 script is to drastically improve performance. An |
| 36 | increase in execution speed of 10 to 100 times can be expected. A secondary |
| 37 | goal is to avoid Vim-specific constructs and get closer to commonly used |
| 38 | programming languages, such as JavaScript, TypeScript and Java. |
| 39 | |
| 40 | The performance improvements can only be achieved by not being 100% backwards |
| 41 | compatible. For example, in a function the arguments are not available in the |
| 42 | "a:" dictionary, as creating that dictionary adds quite a lot of overhead. |
| 43 | Other differences are more subtle, such as how errors are handled. |
| 44 | |
| 45 | The Vim9 script syntax and semantics are used in: |
| 46 | - a function defined with the `:def` command |
| 47 | - a script file where the first command is `vim9script` |
| 48 | |
| 49 | When using `:function` in a Vim9 script file the legacy syntax is used. |
| 50 | However, this is discouraged. |
| 51 | |
| 52 | Vim9 script and legacy Vim script can be mixed. There is no need to rewrite |
| 53 | old scripts, they keep working as before. |
| 54 | |
| 55 | ============================================================================== |
| 56 | |
| 57 | 2. Differences from legacy Vim script *vim9-differences* |
| 58 | |
| 59 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 60 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 61 | Comments starting with # ~ |
| 62 | |
| 63 | In Vim script comments normally start with double quote. That can also be the |
| 64 | start of a string, thus in many places it cannot be used. In Vim9 script a |
| 65 | comment can also start with #. Normally this is a command to list text with |
| 66 | numbers, but you can also use `:number` for that. > |
| 67 | let count = 0 # number of occurences of Ni! |
| 68 | |
| 69 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 70 | Vim9 functions ~ |
| 71 | |
| 72 | `:def` has no extra arguments like `:function` does: "range", "abort", "dict" |
| 73 | or "closure". A `:def` function always aborts on an error, does not get a |
| 74 | range passed and cannot be a "dict" function. |
| 75 | |
| 76 | In the function body: |
| 77 | - Arguments are accessed by name, without "a:". |
| 78 | - There is no "a:" dictionary or "a:000" list. Variable arguments are defined |
| 79 | with a name and have a list type: > |
| 80 | def MyFunc(...itemlist: list<type>) |
| 81 | for item in itemlist |
| 82 | ... |
| 83 | |
| 84 | |
| 85 | Variable declarations with :let and :const ~ |
| 86 | |
| 87 | Local variables need to be declared with `:let`. Local constants need to be |
| 88 | declared with `:const`. We refer to both as "variables". |
| 89 | |
| 90 | Variables can be local to a script, function or code block: > |
| 91 | vim9script |
| 92 | let script_var = 123 |
| 93 | def SomeFunc() |
| 94 | let func_var = script_var |
| 95 | if cond |
| 96 | let block_var = func_var |
| 97 | ... |
| 98 | |
| 99 | The variables are only visible in the block where they are defined and nested |
| 100 | blocks. Once the block ends the variable is no longer accessible: > |
| 101 | if cond |
| 102 | let inner = 5 |
| 103 | else |
| 104 | let inner = 0 |
| 105 | endif |
| 106 | echo inner " Error! |
| 107 | |
| 108 | The declaration must be done earlier: > |
| 109 | let inner: number |
| 110 | if cond |
| 111 | inner = 5 |
| 112 | else |
| 113 | inner = 0 |
| 114 | endif |
| 115 | echo inner |
| 116 | |
| 117 | To intentionally use a variable that won't be available later, a block can be |
| 118 | used: > |
| 119 | { |
| 120 | let temp = 'temp' |
| 121 | ... |
| 122 | } |
| 123 | echo temp " Error! |
| 124 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 125 | An existing variable cannot be assigned to with `:let`, since that implies a |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 126 | declaration. An exception is global variables: these can be both used with |
| 127 | and without `:let`, because there is no rule about where they are declared. |
| 128 | |
| 129 | Variables cannot shadow previously defined variables. |
| 130 | Variables may shadow Ex commands, rename the variable if needed. |
| 131 | |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 132 | Global variables must be prefixed with "g:", also at the script level. |
| 133 | However, global user defined functions are used without "g:". > |
| 134 | vim9script |
| 135 | let script_local = 'text' |
| 136 | let g:global = 'value' |
| 137 | let Funcref = ThatFunction |
| 138 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 139 | Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be |
| 140 | used to repeat a `:substitute` command. |
| 141 | |
| 142 | |
| 143 | Omitting :call and :eval ~ |
| 144 | |
| 145 | Functions can be called without `:call`: > |
| 146 | writefile(lines, 'file') |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 147 | Using `:call` is still possible, but this is discouraged. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 148 | |
| 149 | A method call without `eval` is possible, so long as the start is an |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 150 | identifier or can't be an Ex command. It does NOT work for string constants: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | myList->add(123) " works |
| 152 | g:myList->add(123) " works |
| 153 | [1, 2, 3]->Process() " works |
| 154 | #{a: 1, b: 2}->Process() " works |
| 155 | {'a': 1, 'b': 2}->Process() " works |
| 156 | "foobar"->Process() " does NOT work |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 157 | ("foobar")->Process() " works |
| 158 | 'foobar'->Process() " does NOT work |
| 159 | ('foobar')->Process() " works |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 161 | In case there is ambiguity between a function name and an Ex command, use ":" |
| 162 | to make clear you want to use the Ex command. For example, there is both the |
| 163 | `:substitute` command and the `substitute()` function. When the line starts |
| 164 | with `substitute(` this will use the function, prepend a colon to use the |
| 165 | command instead: > |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 166 | :substitute(pattern (replacement ( |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 167 | |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 168 | Note that while variables need to be defined before they can be used, |
| 169 | functions can be called before being defined. This is required to be able |
| 170 | have cyclic dependencies between functions. It is slightly less efficient, |
| 171 | since the function has to be looked up by name. And a typo in the function |
| 172 | name will only be found when the call is executed. |
| 173 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 174 | |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 175 | Omitting function() ~ |
| 176 | |
| 177 | A user defined function can be used as a function reference in an expression |
| 178 | without `function()`. The argument types and return type will then be checked. |
| 179 | The function must already have been defined. > |
| 180 | |
| 181 | let Funcref = MyFunction |
| 182 | |
| 183 | When using `function()` the resulting type is "func", a function with any |
| 184 | number of arguments and any return type. The function can be defined later. |
| 185 | |
| 186 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 187 | Automatic line continuation ~ |
| 188 | |
| 189 | In many cases it is obvious that an expression continues on the next line. In |
| 190 | those cases there is no need to prefix the line with a backslash. For |
| 191 | example, when a list spans multiple lines: > |
| 192 | let mylist = [ |
| 193 | 'one', |
| 194 | 'two', |
| 195 | ] |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 196 | And when a dict spans multiple lines: > |
| 197 | let mydict = #{ |
| 198 | one: 1, |
| 199 | two: 2, |
| 200 | } |
| 201 | Function call: > |
| 202 | let result = Func( |
| 203 | arg1, |
| 204 | arg2 |
| 205 | ) |
| 206 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 207 | For binary operators iin expressions not in [], {} or () a line break is |
| 208 | possible AFTER the operators. For example: > |
| 209 | let text = lead .. |
| 210 | middle .. |
| 211 | end |
| 212 | let total = start + |
| 213 | end - |
| 214 | correction |
| 215 | let result = positive ? |
| 216 | PosFunc(arg) : |
| 217 | NegFunc(arg) |
| 218 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 219 | Note that "enddef" cannot be used at the start of a continuation line, it ends |
| 220 | the current function. |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 221 | |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 222 | It is also possible to split a function header over multiple lines, in between |
| 223 | arguments: > |
| 224 | def MyFunc( |
| 225 | text: string, |
| 226 | separator = '-' |
| 227 | ): string |
| 228 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 229 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 230 | No curly braces expansion ~ |
| 231 | |
| 232 | |curly-braces-names| cannot be used. |
| 233 | |
| 234 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 235 | No :append, :change or :insert ~ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 236 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 237 | These commands are too quickly confused with local variable names. |
| 238 | |
| 239 | |
| 240 | Comparators ~ |
| 241 | |
| 242 | The 'ignorecase' option is not used for comparators that use strings. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 243 | |
| 244 | |
| 245 | White space ~ |
| 246 | |
| 247 | Vim9 script enforces proper use of white space. This is no longer allowed: > |
| 248 | let var=234 " Error! |
| 249 | let var= 234 " Error! |
| 250 | let var =234 " Error! |
| 251 | There must be white space before and after the "=": > |
| 252 | let var = 234 " OK |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 253 | White space must also be put before the # that starts a comment: > |
| 254 | let var = 234# Error! |
| 255 | let var = 234 # OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 256 | |
| 257 | White space is required around most operators. |
| 258 | |
| 259 | White space is not allowed: |
| 260 | - Between a function name and the "(": > |
| 261 | call Func (arg) " Error! |
| 262 | call Func |
| 263 | \ (arg) " Error! |
| 264 | call Func(arg) " OK |
| 265 | call Func( |
| 266 | \ arg) " OK |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 267 | call Func( |
| 268 | \ arg " OK |
| 269 | \ ) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 270 | |
| 271 | |
| 272 | Conditions and expressions ~ |
| 273 | |
| 274 | Conditions and expression are mostly working like they do in JavaScript. A |
| 275 | difference is made where JavaScript does not work like most people expect. |
| 276 | Specifically, an empty list is falsey. |
| 277 | |
| 278 | Any type of variable can be used as a condition, there is no error, not even |
| 279 | for using a list or job. This is very much like JavaScript, but there are a |
| 280 | few exceptions. |
| 281 | |
| 282 | type TRUE when ~ |
| 283 | bool v:true |
| 284 | number non-zero |
| 285 | float non-zero |
| 286 | string non-empty |
| 287 | blob non-empty |
| 288 | list non-empty (different from JavaScript) |
| 289 | dictionary non-empty (different from JavaScript) |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 290 | func when there is a function name |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 291 | special v:true |
| 292 | job when not NULL |
| 293 | channel when not NULL |
| 294 | class when not NULL |
| 295 | object when not NULL (TODO: when isTrue() returns v:true) |
| 296 | |
| 297 | The boolean operators "||" and "&&" do not change the value: > |
| 298 | 8 || 2 == 8 |
| 299 | 0 || 2 == 2 |
| 300 | 0 || '' == '' |
| 301 | 8 && 2 == 2 |
| 302 | 0 && 2 == 0 |
| 303 | [] && 2 == [] |
| 304 | |
| 305 | When using `..` for string concatenation the arguments are always converted to |
| 306 | string. > |
| 307 | 'hello ' .. 123 == 'hello 123' |
| 308 | 'hello ' .. v:true == 'hello true' |
| 309 | |
| 310 | In Vim9 script one can use "true" for v:true and "false" for v:false. |
| 311 | |
| 312 | |
| 313 | ============================================================================== |
| 314 | |
| 315 | 3. New style functions *fast-functions* |
| 316 | |
| 317 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 318 | |
| 319 | *:def* |
| 320 | :def[!] {name}([arguments])[: {return-type} |
| 321 | Define a new function by the name {name}. The body of |
| 322 | the function follows in the next lines, until the |
| 323 | matching `:enddef`. |
| 324 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 325 | When {return-type} is omitted or is "void" the |
| 326 | function is not expected to return anything. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 327 | |
| 328 | {arguments} is a sequence of zero or more argument |
| 329 | declarations. There are three forms: |
| 330 | {name}: {type} |
| 331 | {name} = {value} |
| 332 | {name}: {type} = {value} |
| 333 | The first form is a mandatory argument, the caller |
| 334 | must always provide them. |
| 335 | The second and third form are optional arguments. |
| 336 | When the caller omits an argument the {value} is used. |
| 337 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 338 | NOTE: It is possible to nest `:def` inside another |
| 339 | `:def`, but it is not possible to nest `:def` inside |
| 340 | `:function`, for backwards compatibility. |
| 341 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 342 | [!] is used as with `:function`. |
| 343 | |
| 344 | *:enddef* |
| 345 | :enddef End of a function defined with `:def`. |
| 346 | |
| 347 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 348 | If the script the function is defined in is Vim9 script, then script-local |
| 349 | variables can be accessed without the "s:" prefix. They must be defined |
| 350 | before the function. If the script the function is defined in is legacy |
| 351 | script, then script-local variables must be accessed with the "s:" prefix. |
| 352 | |
| 353 | |
Bram Moolenaar | ebdf3c9 | 2020-02-15 21:41:42 +0100 | [diff] [blame] | 354 | *:disa* *:disassemble* |
| 355 | :disa[ssemble] {func} Show the instructions generated for {func}. |
| 356 | This is for debugging and testing. |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 357 | Note that for command line completion of {func} you |
| 358 | can prepend "s:" to find script-local functions. |
Bram Moolenaar | ebdf3c9 | 2020-02-15 21:41:42 +0100 | [diff] [blame] | 359 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 360 | ============================================================================== |
| 361 | |
| 362 | 4. Types *vim9-types* |
| 363 | |
| 364 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 365 | |
| 366 | The following builtin types are supported: |
| 367 | bool |
| 368 | number |
| 369 | float |
| 370 | string |
| 371 | blob |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 372 | list<{type}> |
| 373 | dict<{type}> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 374 | job |
| 375 | channel |
Bram Moolenaar | b17893a | 2020-03-14 08:19:51 +0100 | [diff] [blame] | 376 | func |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 377 | func: {type} |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 378 | func({type}, ...) |
| 379 | func({type}, ...): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 380 | |
| 381 | Not supported yet: |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 382 | tuple<a: {type}, b: {type}, ...> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 383 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 384 | These types can be used in declarations, but no value will have this type: |
| 385 | {type}|{type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 386 | void |
| 387 | any |
| 388 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 389 | 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] | 390 | efficient implementation is used that avoids allocating lot of small pieces of |
| 391 | memory. |
| 392 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 393 | A partial and function can be declared in more or less specific ways: |
| 394 | func any kind of function reference, no type |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 395 | checking for arguments or return value |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 396 | func: {type} any number and type of arguments with specific |
| 397 | return type |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 398 | func({type}) function with argument type, does not return |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 399 | a value |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 400 | func({type}): {type} function with argument type and return type |
| 401 | func(?{type}) function with type of optional argument, does |
| 402 | not return a value |
| 403 | func(...{type}) function with type of variable number of |
| 404 | arguments, does not return a value |
| 405 | func({type}, ?{type}, ...{type}): {type} |
| 406 | function with: |
| 407 | - type of mandatory argument |
| 408 | - type of optional argument |
| 409 | - type of variable number of arguments |
| 410 | - return type |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 411 | |
| 412 | If the return type is "void" the function does not return a value. |
| 413 | |
| 414 | The reference can also be a |Partial|, in which case it stores extra arguments |
| 415 | and/or a dictionary, which are not visible to the caller. Since they are |
| 416 | called in the same way the declaration is the same. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 417 | |
| 418 | Custom types can be defined with `:type`: > |
| 419 | :type MyList list<string> |
| 420 | {not implemented yet} |
| 421 | |
| 422 | And classes and interfaces can be used as types: > |
| 423 | :class MyClass |
| 424 | :let mine: MyClass |
| 425 | |
| 426 | :interface MyInterface |
| 427 | :let mine: MyInterface |
| 428 | |
| 429 | :class MyTemplate<Targ> |
| 430 | :let mine: MyTemplate<number> |
| 431 | :let mine: MyTemplate<string> |
| 432 | |
| 433 | :class MyInterface<Targ> |
| 434 | :let mine: MyInterface<number> |
| 435 | :let mine: MyInterface<string> |
| 436 | {not implemented yet} |
| 437 | |
| 438 | |
| 439 | Type inference *type-inference* |
| 440 | |
| 441 | In general: Whenever the type is clear it can be omitted. For example, when |
| 442 | declaring a variable and giving it a value: > |
| 443 | let var = 0 " infers number type |
| 444 | let var = 'hello' " infers string type |
| 445 | |
| 446 | |
| 447 | ============================================================================== |
| 448 | |
| 449 | 5. Namespace, Import and Export |
| 450 | *vim9script* *vim9-export* *vim9-import* |
| 451 | |
| 452 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 453 | |
| 454 | A Vim9 script can be written to be imported. This means that everything in |
| 455 | the script is local, unless exported. Those exported items, and only those |
| 456 | items, can then be imported in another script. |
| 457 | |
| 458 | |
| 459 | Namespace ~ |
| 460 | *:vim9script* *:vim9* |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 461 | To recognize a file that can be imported the `vim9script` statement must |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 462 | appear as the first statement in the file. It tells Vim to interpret the |
| 463 | script in its own namespace, instead of the global namespace. If a file |
| 464 | starts with: > |
| 465 | vim9script |
| 466 | let myvar = 'yes' |
| 467 | Then "myvar" will only exist in this file. While without `vim9script` it would |
| 468 | be available as `g:myvar` from any other script and function. |
| 469 | |
| 470 | The variables at the file level are very much like the script-local "s:" |
| 471 | variables in legacy Vim script, but the "s:" is omitted. |
| 472 | |
| 473 | In Vim9 script the global "g:" namespace can still be used as before. |
| 474 | |
| 475 | A side effect of `:vim9script` is that the 'cpoptions' option is set to the |
| 476 | Vim default value, like with: > |
| 477 | :set cpo&vim |
| 478 | One of the effects is that |line-continuation| is always enabled. |
| 479 | The original value of 'cpoptions' is restored at the end of the script. |
| 480 | |
| 481 | |
| 482 | Export ~ |
| 483 | *:export* *:exp* |
| 484 | Exporting one item can be written as: > |
| 485 | export const EXPORTED_CONST = 1234 |
| 486 | export let someValue = ... |
| 487 | export def MyFunc() ... |
| 488 | export class MyClass ... |
| 489 | |
| 490 | As this suggests, only constants, variables, `:def` functions and classes can |
| 491 | be exported. |
| 492 | |
| 493 | Alternatively, an export statement can be used to export several already |
| 494 | defined (otherwise script-local) items: > |
| 495 | export {EXPORTED_CONST, someValue, MyFunc, MyClass} |
| 496 | |
| 497 | |
| 498 | Import ~ |
| 499 | *:import* *:imp* |
| 500 | The exported items can be imported individually in another Vim9 script: > |
| 501 | import EXPORTED_CONST from "thatscript.vim" |
| 502 | import MyClass from "myclass.vim" |
| 503 | |
| 504 | To import multiple items at the same time: > |
| 505 | import {someValue, MyClass} from "thatscript.vim" |
| 506 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 507 | In case the name is ambiguous, another name can be specified: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 508 | import MyClass as ThatClass from "myclass.vim" |
| 509 | import {someValue, MyClass as ThatClass} from "myclass.vim" |
| 510 | |
| 511 | To import all exported items under a specific identifier: > |
| 512 | import * as That from 'thatscript.vim' |
| 513 | |
| 514 | Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free |
| 515 | to choose the name "That", but it is highly recommended to use the name of the |
| 516 | script file to avoid confusion. |
| 517 | |
| 518 | The script name after `import` can be: |
| 519 | - A relative path, starting "." or "..". This finds a file relative to the |
| 520 | location of the script file itself. This is useful to split up a large |
| 521 | plugin into several files. |
| 522 | - An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This |
| 523 | will be rarely used. |
| 524 | - A path not being relative or absolute. This will be found in the |
| 525 | "import" subdirectories of 'runtimepath' entries. The name will usually be |
| 526 | longer and unique, to avoid loading the wrong file. |
| 527 | |
| 528 | Once a vim9 script file has been imported, the result is cached and used the |
| 529 | next time the same script is imported. It will not be read again. |
| 530 | *:import-cycle* |
| 531 | The `import` commands are executed when encountered. If that script (directly |
| 532 | or indirectly) imports the current script, then items defined after the |
| 533 | `import` won't be processed yet. Therefore cyclic imports can exist, but may |
| 534 | result in undefined items. |
| 535 | |
| 536 | |
| 537 | Import in an autoload script ~ |
| 538 | |
| 539 | For optimal startup speed, loading scripts should be postponed until they are |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 540 | actually needed. A recommended mechanism: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 541 | |
| 542 | 1. In the plugin define user commands, functions and/or mappings that refer to |
| 543 | an autoload script. > |
| 544 | command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>) |
| 545 | |
| 546 | < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. |
| 547 | |
| 548 | 2. In the autocommand script do the actual work. You can import items from |
| 549 | other files to split up functionality in appropriate pieces. > |
| 550 | vim9script |
| 551 | import FilterFunc from "../import/someother.vim" |
| 552 | def searchfor#Stuff(arg: string) |
| 553 | let filtered = FilterFunc(arg) |
| 554 | ... |
| 555 | < This goes in .../autoload/searchfor.vim. "searchfor" in the file name |
| 556 | must be exactly the same as the prefix for the function name, that is how |
| 557 | Vim finds the file. |
| 558 | |
| 559 | 3. Other functionality, possibly shared between plugins, contains the exported |
| 560 | items and any private items. > |
| 561 | vim9script |
| 562 | let localVar = 'local' |
| 563 | export def FilterFunc(arg: string): string |
| 564 | ... |
| 565 | < This goes in .../import/someother.vim. |
| 566 | |
| 567 | |
| 568 | Import in legacy Vim script ~ |
| 569 | |
| 570 | If an `import` statement is used in legacy Vim script, for identifier the |
| 571 | script-local "s:" namespace will be used, even when "s:" is not specified. |
| 572 | |
| 573 | |
| 574 | ============================================================================== |
| 575 | |
| 576 | 9. Rationale *vim9-rationale* |
| 577 | |
| 578 | The :def command ~ |
| 579 | |
| 580 | Plugin writers have asked for a much faster Vim script. Investigation have |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 581 | 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] | 582 | impossible, because of the overhead involved with calling a function, setting |
| 583 | up the local function scope and executing lines. There are many details that |
| 584 | need to be handled, such as error messages and exceptions. The need to create |
| 585 | a dictionary for a: and l: scopes, the a:000 list and several others add too |
| 586 | much overhead that cannot be avoided. |
| 587 | |
| 588 | Therefore the `:def` method to define a new-style function had to be added, |
| 589 | which allows for a function with different semantics. Most things still work |
| 590 | as before, but some parts do not. A new way to define a function was |
| 591 | considered the best way to separate the old-style code from Vim9 script code. |
| 592 | |
| 593 | Using "def" to define a function comes from Python. Other languages use |
| 594 | "function" which clashes with legacy Vim script. |
| 595 | |
| 596 | |
| 597 | Type checking ~ |
| 598 | |
| 599 | When compiling lines of Vim commands into instructions as much as possible |
| 600 | should be done at compile time. Postponing it to runtime makes the execution |
| 601 | slower and means mistakes are found only later. For example, when |
| 602 | encountering the "+" character and compiling this into a generic add |
| 603 | instruction, at execution time the instruction would have to inspect the type |
| 604 | of the arguments and decide what kind of addition to do. And when the |
| 605 | type is dictionary throw an error. If the types are known to be numbers then |
| 606 | an "add number" instruction can be used, which is faster. The error can be |
| 607 | given at compile time, no error handling is needed at runtime. |
| 608 | |
| 609 | The syntax for types is similar to Java, since it is easy to understand and |
| 610 | widely used. The type names are what was used in Vim before, with some |
| 611 | additions such as "void" and "bool". |
| 612 | |
| 613 | |
| 614 | JavaScript/TypeScript syntax and semantics ~ |
| 615 | |
| 616 | Script writers have complained that the Vim script syntax is unexpectedly |
| 617 | different from what they are used to. To reduce this complaint popular |
| 618 | languages will be used as an example. At the same time, we do not want to |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 619 | abandon the well-known parts of legacy Vim script. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 620 | |
| 621 | Since Vim already uses `:let` and `:const` and optional type checking is |
| 622 | desirable, the JavaScript/TypeScript syntax fits best for variable |
| 623 | declarations. > |
| 624 | const greeting = 'hello' " string type is inferred |
| 625 | let name: string |
| 626 | ... |
| 627 | name = 'John' |
| 628 | |
| 629 | Expression evaluation was already close to what JavaScript and other languages |
| 630 | are doing. Some details are unexpected and can be fixed. For example how the |
| 631 | || and && operators work. Legacy Vim script: > |
| 632 | let result = 44 |
| 633 | ... |
| 634 | return result || 0 " returns 1 |
| 635 | |
| 636 | Vim9 script works like JavaScript, keep the value: > |
| 637 | let result = 44 |
| 638 | ... |
| 639 | return result || 0 " returns 44 |
| 640 | |
| 641 | On the other hand, overloading "+" to use both for addition and string |
| 642 | concatenation goes against legacy Vim script and often leads to mistakes. |
| 643 | For that reason we will keep using ".." for string concatenation. Lua also |
| 644 | uses ".." this way. |
| 645 | |
| 646 | |
| 647 | Import and Export ~ |
| 648 | |
| 649 | A problem of legacy Vim script is that by default all functions and variables |
| 650 | are global. It is possible to make them script-local, but then they are not |
| 651 | available in other scripts. |
| 652 | |
| 653 | In Vim9 script a mechanism very similar to the Javascript import and export |
| 654 | mechanism is supported. It is a variant to the existing `:source` command |
| 655 | that works like one would expect: |
| 656 | - Instead of making everything global by default, everything is script-local, |
| 657 | unless exported. |
| 658 | - When importing a script the symbols that are imported are listed, avoiding |
| 659 | name conflicts and failures if later functionality is added. |
| 660 | - The mechanism allows for writing a big, long script with a very clear API: |
| 661 | the exported function(s) and class(es). |
| 662 | - By using relative paths loading can be much faster for an import inside of a |
| 663 | package, no need to search many directories. |
| 664 | - Once an import has been used, it can be cached and loading it again can be |
| 665 | avoided. |
| 666 | - The Vim-specific use of "s:" to make things script-local can be dropped. |
| 667 | |
| 668 | |
| 669 | Classes ~ |
| 670 | |
| 671 | Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But |
| 672 | these have never become widespread. When Vim 9 was designed a decision was |
| 673 | made to phase out these interfaces and concentrate on Vim script, while |
| 674 | encouraging plugin authors to write code in any language and run it as an |
| 675 | external tool, using jobs and channels. |
| 676 | |
| 677 | Still, using an external tool has disadvantages. An alternative is to convert |
| 678 | the tool into Vim script. For that to be possible without too much |
| 679 | translation, and keeping the code fast at the same time, the constructs of the |
| 680 | tool need to be supported. Since most languages support classes the lack of |
| 681 | class support in Vim is then a problem. |
| 682 | |
| 683 | Previously Vim supported a kind-of object oriented programming by adding |
| 684 | methods to a dictionary. With some care this could be made to work, but it |
| 685 | does not look like real classes. On top of that, it's very slow, because of |
| 686 | the use of dictionaries. |
| 687 | |
| 688 | The support of classes in Vim9 script is a "minimal common functionality" of |
| 689 | class support in most languages. It works mostly like Java, which is the most |
| 690 | popular programming language. |
| 691 | |
| 692 | |
| 693 | |
| 694 | vim:tw=78:ts=8:noet:ft=help:norl: |