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