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