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