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