Mercurial > vim
annotate runtime/doc/vim9.txt @ 24243:14bd3e0d0c71
Added tag v8.2.2662 for changeset d3cabf12c991b586a2337fa7116641e60b466bad
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 27 Mar 2021 12:15:05 +0100 |
parents | 4919f2d8d7fd |
children | cabed216cc2f |
rev | line source |
---|---|
24103 | 1 *vim9.txt* For Vim version 8.2. Last change: 2021 Mar 03 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
2 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
3 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
4 VIM REFERENCE MANUAL by Bram Moolenaar |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
5 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
6 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
7 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
8 |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
9 Vim9 script commands and expressions. *Vim9* *vim9* |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
10 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
11 Most expression help is in |eval.txt|. This file is about the new syntax and |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
12 features in Vim9 script. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
13 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
14 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
15 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
16 |
23466 | 17 1. What is Vim9 script? |Vim9-script| |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
18 2. Differences |vim9-differences| |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
19 3. New style functions |fast-functions| |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
20 4. Types |vim9-types| |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
21 5. Namespace, Import and Export |vim9script| |
22328 | 22 6. Future work: classes |vim9-classes| |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
23 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
24 9. Rationale |vim9-rationale| |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
25 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
26 ============================================================================== |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
27 |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
28 1. What is Vim9 script? *Vim9-script* |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
29 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
30 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
31 |
20241 | 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 | |
20965 | 34 and compatibility with Vi restricts possible solutions. Execution is quite |
20241 | 35 slow, each line is parsed every time it is executed. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
36 |
20241 | 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. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
43 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
44 The performance improvements can only be achieved by not being 100% backwards |
20552 | 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. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
49 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
50 The Vim9 script syntax and semantics are used in: |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
51 - a function defined with the `:def` command |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
52 - a script file where the first command is `vim9script` |
22328 | 53 - an autocommand defined in the context of the above |
23938
2a885d095bff
patch 8.2.2511: Vim9: cannot use Vim9 script syntax in some places
Bram Moolenaar <Bram@vim.org>
parents:
23931
diff
changeset
|
54 - a command prefixed with the `vim9cmd` command modifier |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
55 |
23573 | 56 When using `:function` in a Vim9 script file the legacy syntax is used, with |
57 the highest |scriptversion|. However, this can be confusing and is therefore | |
58 discouraged. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
59 |
20241 | 60 Vim9 script and legacy Vim script can be mixed. There is no requirement to |
22328 | 61 rewrite old scripts, they keep working as before. You may want to use a few |
62 `:def` functions for code that needs to be fast. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
63 |
23938
2a885d095bff
patch 8.2.2511: Vim9: cannot use Vim9 script syntax in some places
Bram Moolenaar <Bram@vim.org>
parents:
23931
diff
changeset
|
64 *:vim9* *:vim9cmd* |
2a885d095bff
patch 8.2.2511: Vim9: cannot use Vim9 script syntax in some places
Bram Moolenaar <Bram@vim.org>
parents:
23931
diff
changeset
|
65 :vim9[cmd] {cmd} |
2a885d095bff
patch 8.2.2511: Vim9: cannot use Vim9 script syntax in some places
Bram Moolenaar <Bram@vim.org>
parents:
23931
diff
changeset
|
66 Execute {cmd} using Vim9 script syntax and semantics. |
2a885d095bff
patch 8.2.2511: Vim9: cannot use Vim9 script syntax in some places
Bram Moolenaar <Bram@vim.org>
parents:
23931
diff
changeset
|
67 Useful when typing a command and in a legacy script or |
2a885d095bff
patch 8.2.2511: Vim9: cannot use Vim9 script syntax in some places
Bram Moolenaar <Bram@vim.org>
parents:
23931
diff
changeset
|
68 function. |
2a885d095bff
patch 8.2.2511: Vim9: cannot use Vim9 script syntax in some places
Bram Moolenaar <Bram@vim.org>
parents:
23931
diff
changeset
|
69 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
70 ============================================================================== |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
71 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
72 2. Differences from legacy Vim script *vim9-differences* |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
73 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
74 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
75 |
22441 | 76 Overview ~ |
77 | |
78 Brief summary of the differences you will most often encounter when using Vim9 | |
79 script and `:def` functions; details are below: | |
80 - Comments start with #, not ": > | |
23573 | 81 echo "hello" # comment |
22441 | 82 - Using a backslash for line continuation is hardly ever needed: > |
23573 | 83 echo "hello " |
22441 | 84 .. yourName |
85 .. ", how are you?" | |
86 - White space is required in many places. | |
87 - Assign values without `:let`, declare variables with `:var`: > | |
23573 | 88 var count = 0 |
22441 | 89 count += 3 |
90 - Constants can be declared with `:final` and `:const`: > | |
23573 | 91 final matches = [] # add matches |
22441 | 92 const names = ['Betty', 'Peter'] # cannot be changed |
93 - `:final` cannot be used as an abbreviation of `:finally`. | |
94 - Variables and functions are script-local by default. | |
95 - Functions are declared with argument types and return type: > | |
96 def CallMe(count: number, message: string): bool | |
97 - Call functions without `:call`: > | |
23573 | 98 writefile(['done'], 'file.txt') |
24128
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
99 - You cannot use `:xit`, `:t`, `:k`, `:append`, `:change`, `:insert`, `:open` |
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
100 or curly-braces names. |
22441 | 101 - A range before a command must be prefixed with a colon: > |
23573 | 102 :%s/this/that |
103 - Unless mentioned specifically, the highest |scriptversion| is used. | |
22441 | 104 |
105 | |
20023
c85d4e173cc9
patch 8.2.0567: Vim9: cannot put comments halfway expressions
Bram Moolenaar <Bram@vim.org>
parents:
20015
diff
changeset
|
106 Comments starting with # ~ |
c85d4e173cc9
patch 8.2.0567: Vim9: cannot put comments halfway expressions
Bram Moolenaar <Bram@vim.org>
parents:
20015
diff
changeset
|
107 |
21353
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
108 In legacy Vim script comments start with double quote. In Vim9 script |
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
109 comments start with #. > |
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
110 # declarations |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
111 var count = 0 # number of occurrences |
20023
c85d4e173cc9
patch 8.2.0567: Vim9: cannot put comments halfway expressions
Bram Moolenaar <Bram@vim.org>
parents:
20015
diff
changeset
|
112 |
21353
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
113 The reason is that a double quote can also be the start of a string. In many |
21825 | 114 places, especially halfway through an expression with a line break, it's hard |
115 to tell what the meaning is, since both a string and a comment can be followed | |
116 by arbitrary text. To avoid confusion only # comments are recognized. This | |
117 is the same as in shell scripts and Python programs. | |
21353
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
118 |
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
119 In Vi # is a command to list text with numbers. In Vim9 script you can use |
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
120 `:number` for that. > |
21516
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
121 101 number |
21353
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
122 |
fb8c8fcb7b60
patch 8.2.1227: Vim9: allowing both quoted and # comments is confusing
Bram Moolenaar <Bram@vim.org>
parents:
21250
diff
changeset
|
123 To improve readability there must be a space between a command and the # |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
124 that starts a comment: > |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
125 var name = value # comment |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
126 var name = value# error! |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
127 |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
128 In legacy Vim script # is also used for the alternate file name. In Vim9 |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
129 script you need to use %% instead. Instead of ## use %%% (stands for all |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
130 arguments). |
20115 | 131 |
20023
c85d4e173cc9
patch 8.2.0567: Vim9: cannot put comments halfway expressions
Bram Moolenaar <Bram@vim.org>
parents:
20015
diff
changeset
|
132 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
133 Vim9 functions ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
134 |
20241 | 135 A function defined with `:def` is compiled. Execution is many times faster, |
136 often 10x to 100x times. | |
137 | |
20552 | 138 Many errors are already found when compiling, before the function is executed. |
20241 | 139 The syntax is strict, to enforce code that is easy to read and understand. |
140 | |
23164 | 141 Compilation is done when either of these is encountered: |
142 - the first time the function is called | |
21991 | 143 - when the `:defcompile` command is encountered in the script where the |
144 function was defined | |
145 - `:disassemble` is used for the function. | |
146 - a function that is compiled calls the function or uses it as a function | |
147 reference | |
20552 | 148 |
149 `:def` has no options like `:function` does: "range", "abort", "dict" or | |
23164 | 150 "closure". A `:def` function always aborts on an error (unless `:silent!` was |
151 used for the command or inside a `:try` block), does not get a range passed | |
23305 | 152 cannot be a "dict" function, and can always be a closure. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
153 |
20241 | 154 The argument types and return type need to be specified. The "any" type can |
155 be used, type checking will then be done at runtime, like with legacy | |
156 functions. | |
157 | |
21825 | 158 Arguments are accessed by name, without "a:", just like any other language. |
159 There is no "a:" dictionary or "a:000" list. | |
20241 | 160 |
161 Variable arguments are defined as the last argument, with a name and have a | |
21825 | 162 list type, similar to TypeScript. For example, a list of numbers: > |
22171 | 163 def MyFunc(...itemlist: list<number>) |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
164 for item in itemlist |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
165 ... |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
166 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
167 |
20241 | 168 Functions and variables are script-local by default ~ |
20856 | 169 *vim9-scopes* |
20115 | 170 When using `:function` or `:def` to specify a new function at the script level |
171 in a Vim9 script, the function is local to the script, as if "s:" was | |
22494
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
172 prefixed. Using the "s:" prefix is optional. To define a global function or |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
173 variable the "g:" prefix must be used. For functions in an autoload script |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
174 the "name#" prefix is sufficient. > |
21550
b0b57d91671c
patch 8.2.1325: Vim9: using Vim9 script for autaload not tested
Bram Moolenaar <Bram@vim.org>
parents:
21516
diff
changeset
|
175 def ThisFunction() # script-local |
b0b57d91671c
patch 8.2.1325: Vim9: using Vim9 script for autaload not tested
Bram Moolenaar <Bram@vim.org>
parents:
21516
diff
changeset
|
176 def s:ThisFunction() # script-local |
22171 | 177 def g:ThatFunction() # global |
21550
b0b57d91671c
patch 8.2.1325: Vim9: using Vim9 script for autaload not tested
Bram Moolenaar <Bram@vim.org>
parents:
21516
diff
changeset
|
178 def scriptname#function() # autoload |
20115 | 179 |
22494
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
180 When using `:function` or `:def` to specify a nested function inside a `:def` |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
181 function, this nested function is local to the code block it is defined in. |
22565 | 182 In a `:def` function it is not possible to define a script-local function. It |
22494
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
183 is possible to define a global function by using the "g:" prefix. |
20115 | 184 |
185 When referring to a function and no "s:" or "g:" prefix is used, Vim will | |
22500
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
186 search for the function: |
22565 | 187 - in the function scope, in block scopes |
22500
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
188 - in the script scope, possibly imported |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
189 - in the list of global functions |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
190 However, it is recommended to always use "g:" to refer to a global function |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
191 for clarity. |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
192 |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
193 In all cases the function must be defined before used. That is when it is |
22723 | 194 called, when `:defcompile` causes it to be compiled, or when code that calls |
195 it is being compiled (to figure out the return type). | |
20241 | 196 |
21676 | 197 The result is that functions and variables without a namespace can usually be |
20241 | 198 found in the script, either defined there or imported. Global functions and |
21676 | 199 variables could be defined anywhere (good luck finding out where!). |
20115 | 200 |
21825 | 201 Global functions can still be defined and deleted at nearly any time. In |
20317 | 202 Vim9 script script-local functions are defined once when the script is sourced |
20552 | 203 and cannot be deleted or replaced. |
20115 | 204 |
23305 | 205 When compiling a function and a function call is encountered for a function |
206 that is not (yet) defined, the |FuncUndefined| autocommand is not triggered. | |
207 You can use an autoload function if needed, or call a legacy function and have | |
208 |FuncUndefined| triggered there. | |
209 | |
20115 | 210 |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
211 Reloading a Vim9 script clears functions and variables by default ~ |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
212 *vim9-reload* |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
213 When loading a legacy Vim script a second time nothing is removed, the |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
214 commands will replace existing variables and functions and create new ones. |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
215 |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
216 When loading a Vim9 script a second time all existing script-local functions |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
217 and variables are deleted, thus you start with a clean slate. This is useful |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
218 if you are developing a plugin and want to try a new version. If you renamed |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
219 something you don't have to worry about the old name still hanging around. |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
220 |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
221 If you do want to keep items, use: > |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
222 vim9script noclear |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
223 |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
224 You want to use this in scripts that use a `finish` command to bail out at |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
225 some point when loaded again. E.g. when a buffer local option is set: > |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
226 vim9script noclear |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
227 setlocal completefunc=SomeFunc |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
228 if exists('*g:SomeFunc') | finish | endif |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
229 def g:SomeFunc() |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
230 .... |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
231 |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
232 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
233 Variable declarations with :var, :final and :const ~ |
22494
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
234 *vim9-declaration* *:var* |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
235 Local variables need to be declared with `:var`. Local constants need to be |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
236 declared with `:final` or `:const`. We refer to both as "variables" in this |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
237 section. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
238 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
239 Variables can be local to a script, function or code block: > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
240 vim9script |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
241 var script_var = 123 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
242 def SomeFunc() |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
243 var func_var = script_var |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
244 if cond |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
245 var block_var = func_var |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
246 ... |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
247 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
248 The variables are only visible in the block where they are defined and nested |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
249 blocks. Once the block ends the variable is no longer accessible: > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
250 if cond |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
251 var inner = 5 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
252 else |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
253 var inner = 0 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
254 endif |
22171 | 255 echo inner # Error! |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
256 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
257 The declaration must be done earlier: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
258 var inner: number |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
259 if cond |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
260 inner = 5 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
261 else |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
262 inner = 0 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
263 endif |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
264 echo inner |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
265 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
266 To intentionally hide a variable from code that follows, a block can be |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
267 used: > |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
268 { |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
269 var temp = 'temp' |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
270 ... |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
271 } |
22171 | 272 echo temp # Error! |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
273 |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
274 Declaring a variable with a type but without an initializer will initialize to |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
275 zero, false or empty. |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
276 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
277 In Vim9 script `:let` cannot be used. An existing variable is assigned to |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
278 without any command. The same for global, window, tab, buffer and Vim |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
279 variables, because they are not really declared. They can also be deleted |
21584
d0c76ce48326
patch 8.2.1342: Vim9: accidentally using "t" gives a confusing error
Bram Moolenaar <Bram@vim.org>
parents:
21550
diff
changeset
|
280 with `:unlet`. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
281 |
24024 | 282 Variables, functions and function arguments cannot shadow previously defined |
283 or imported variables and functions in the same script file. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
284 Variables may shadow Ex commands, rename the variable if needed. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
285 |
20241 | 286 Global variables and user defined functions must be prefixed with "g:", also |
287 at the script level. > | |
19968 | 288 vim9script |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
289 var script_local = 'text' |
21499 | 290 g:global = 'value' |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
291 var Funcref = g:ThatFunction |
19968 | 292 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
293 Since `&opt = value` is now assigning a value to option "opt", ":&" cannot be |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
294 used to repeat a `:substitute` command. |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
295 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
296 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
297 Constants ~ |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
298 *vim9-const* *vim9-final* |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
299 How constants work varies between languages. Some consider a variable that |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
300 can't be assigned another value a constant. JavaScript is an example. Others |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
301 also make the value immutable, thus when a constant uses a list, the list |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
302 cannot be changed. In Vim9 we can use both. |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
303 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
304 `:const` is used for making both the variable and the value a constant. Use |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
305 this for composite structures that you want to make sure will not be modified. |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
306 Example: > |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
307 const myList = [1, 2] |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
308 myList = [3, 4] # Error! |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
309 myList[0] = 9 # Error! |
24024 | 310 myList->add(3) # Error! |
22494
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
311 < *:final* |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
312 `:final` is used for making only the variable a constant, the value can be |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
313 changed. This is well known from Java. Example: > |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
314 final myList = [1, 2] |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
315 myList = [3, 4] # Error! |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
316 myList[0] = 9 # OK |
24024 | 317 myList->add(3) # OK |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
318 |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
319 It is common to write constants as ALL_CAPS, but you don't have to. |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
320 |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
321 The constant only applies to the value itself, not what it refers to. > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
322 final females = ["Mary"] |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
323 const NAMES = [["John", "Peter"], females] |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
324 NAMES[0] = ["Jack"] # Error! |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
325 NAMES[0][0] = "Jack" # Error! |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
326 NAMES[1] = ["Emma"] # Error! |
23573 | 327 NAMES[1][0] = "Emma" # OK, now females[0] == "Emma" |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
328 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
329 < *E1092* |
21499 | 330 Declaring more than one variable at a time, using the unpack notation, is |
331 currently not supported: > | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
332 var [v1, v2] = GetValues() # Error! |
21499 | 333 That is because the type needs to be inferred from the list item type, which |
334 isn't that easy. | |
335 | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
336 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
337 Omitting :call and :eval ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
338 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
339 Functions can be called without `:call`: > |
22171 | 340 writefile(lines, 'file') |
19303 | 341 Using `:call` is still possible, but this is discouraged. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
342 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
343 A method call without `eval` is possible, so long as the start is an |
24128
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
344 identifier or can't be an Ex command. For a function either "(" or "->" must |
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
345 be following, without a line break. Examples: > |
21516
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
346 myList->add(123) |
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
347 g:myList->add(123) |
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
348 [1, 2, 3]->Process() |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
349 {a: 1, b: 2}->Process() |
21516
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
350 "foobar"->Process() |
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
351 ("foobar")->Process() |
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
352 'foobar'->Process() |
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
353 ('foobar')->Process() |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
354 |
21825 | 355 In the rare case there is ambiguity between a function name and an Ex command, |
21676 | 356 prepend ":" to make clear you want to use the Ex command. For example, there |
357 is both the `:substitute` command and the `substitute()` function. When the | |
358 line starts with `substitute(` this will use the function. Prepend a colon to | |
359 use the command instead: > | |
19481
c27837cbe922
patch 8.2.0298: Vim9 script: cannot start command with a string constant
Bram Moolenaar <Bram@vim.org>
parents:
19473
diff
changeset
|
360 :substitute(pattern (replacement ( |
19473
b09afbebffee
patch 8.2.0294: cannot use Ex command that is also a function name
Bram Moolenaar <Bram@vim.org>
parents:
19404
diff
changeset
|
361 |
19556
ff5048b0ccfe
patch 8.2.0335: no completion for :disassemble
Bram Moolenaar <Bram@vim.org>
parents:
19523
diff
changeset
|
362 Note that while variables need to be defined before they can be used, |
21825 | 363 functions can be called before being defined. This is required to allow |
364 for cyclic dependencies between functions. It is slightly less efficient, | |
19556
ff5048b0ccfe
patch 8.2.0335: no completion for :disassemble
Bram Moolenaar <Bram@vim.org>
parents:
19523
diff
changeset
|
365 since the function has to be looked up by name. And a typo in the function |
21516
c7b2ce90c2de
patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents:
21499
diff
changeset
|
366 name will only be found when the function is called. |
19556
ff5048b0ccfe
patch 8.2.0335: no completion for :disassemble
Bram Moolenaar <Bram@vim.org>
parents:
19523
diff
changeset
|
367 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
368 |
19968 | 369 Omitting function() ~ |
370 | |
371 A user defined function can be used as a function reference in an expression | |
372 without `function()`. The argument types and return type will then be checked. | |
373 The function must already have been defined. > | |
374 | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
375 var Funcref = MyFunction |
19968 | 376 |
377 When using `function()` the resulting type is "func", a function with any | |
378 number of arguments and any return type. The function can be defined later. | |
379 | |
380 | |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
381 Lambda using => instead of -> ~ |
23318
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
382 |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
383 In legacy script there can be confusion between using "->" for a method call |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
384 and for a lambda. Also, when a "{" is found the parser needs to figure out if |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
385 it is the start of a lambda or a dictionary, which is now more complicated |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
386 because of the use of argument types. |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
387 |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
388 To avoid these problems Vim9 script uses a different syntax for a lambda, |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
389 which is similar to Javascript: > |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
390 var Lambda = (arg) => expression |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
391 |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
392 No line break is allowed in the arguments of a lambda up to and including the |
23318
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
393 "=>". This is OK: > |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
394 filter(list, (k, v) => |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
395 v > 0) |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
396 This does not work: > |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
397 filter(list, (k, v) |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
398 => v > 0) |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
399 This also does not work: > |
23318
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
400 filter(list, (k, |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
401 v) => v > 0) |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
402 But you can use a backslash to concatenate the lines before parsing: > |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
403 filter(list, (k, |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
404 \ v) |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
405 \ => v > 0) |
23318
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
406 |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
407 Additionally, a lambda can contain statements in {}: > |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
408 var Lambda = (arg) => { |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
409 g:was_called = 'yes' |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
410 return expression |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
411 } |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
412 NOT IMPLEMENTED YET |
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
413 |
23737 | 414 *vim9-curly* |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
415 To avoid the "{" of a dictionary literal to be recognized as a statement block |
24024 | 416 wrap it in parentheses: > |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
417 var Lambda = (arg) => ({key: 42}) |
23318
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
418 |
23737 | 419 Also when confused with the start of a command block: > |
420 ({ | |
421 key: value | |
422 })->method() | |
423 | |
23318
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
424 |
19999
844c7646f61b
patch 8.2.0555: Vim9: line continuation is not always needed
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
425 Automatic line continuation ~ |
844c7646f61b
patch 8.2.0555: Vim9: line continuation is not always needed
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
426 |
844c7646f61b
patch 8.2.0555: Vim9: line continuation is not always needed
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
427 In many cases it is obvious that an expression continues on the next line. In |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
428 those cases there is no need to prefix the line with a backslash (see |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
429 |line-continuation|). For example, when a list spans multiple lines: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
430 var mylist = [ |
19999
844c7646f61b
patch 8.2.0555: Vim9: line continuation is not always needed
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
431 'one', |
844c7646f61b
patch 8.2.0555: Vim9: line continuation is not always needed
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
432 'two', |
844c7646f61b
patch 8.2.0555: Vim9: line continuation is not always needed
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
433 ] |
20011
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
434 And when a dict spans multiple lines: > |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
435 var mydict = { |
20011
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
436 one: 1, |
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
437 two: 2, |
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
438 } |
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
439 Function call: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
440 var result = Func( |
20011
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
441 arg1, |
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
442 arg2 |
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
443 ) |
628011800942
patch 8.2.0561: Vim9: cannot split function call in multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
19999
diff
changeset
|
444 |
20982
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
445 For binary operators in expressions not in [], {} or () a line break is |
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
446 possible just before or after the operator. For example: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
447 var text = lead |
20982
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
448 .. middle |
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
449 .. end |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
450 var total = start + |
23573 | 451 end - |
20013
bf377a9ffccb
patch 8.2.0562: Vim9: cannot split an expression into multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
20011
diff
changeset
|
452 correction |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
453 var result = positive |
20982
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
454 ? PosFunc(arg) |
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
455 : NegFunc(arg) |
20013
bf377a9ffccb
patch 8.2.0562: Vim9: cannot split an expression into multiple lines
Bram Moolenaar <Bram@vim.org>
parents:
20011
diff
changeset
|
456 |
21499 | 457 For a method call using "->" and a member using a dot, a line break is allowed |
458 before it: > | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
459 var result = GetBuilder() |
20965 | 460 ->BuilderSetWidth(333) |
461 ->BuilderSetHeight(777) | |
462 ->BuilderBuild() | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
463 var result = MyDict |
21499 | 464 .member |
20965 | 465 |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
466 For commands that have an argument that is a list of commands, the | character |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
467 at the start of the line indicates line continuation: > |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
468 autocmd BufNewFile *.match if condition |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
469 | echo 'match' |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
470 | endif |
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
471 |
20982
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
472 < *E1050* |
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
473 To make it possible for the operator at the start of the line to be |
21250 | 474 recognized, it is required to put a colon before a range. This will add |
20982
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
475 "start" and print: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
476 var result = start |
20982
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
477 + print |
21250 | 478 Like this: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
479 var result = start + print |
21250 | 480 |
20982
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
481 This will assign "start" and print a line: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
482 var result = start |
20982
bb49b5090a9c
patch 8.2.1042: Vim9: cannot put an operator on the next line
Bram Moolenaar <Bram@vim.org>
parents:
20965
diff
changeset
|
483 :+ print |
19999
844c7646f61b
patch 8.2.0555: Vim9: line continuation is not always needed
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
484 |
23047 | 485 Note that the colon is not required for the |+cmd| argument: > |
486 edit +6 fname | |
487 | |
20015
c001ee73519a
patch 8.2.0563: Vim9: cannot split a function line
Bram Moolenaar <Bram@vim.org>
parents:
20013
diff
changeset
|
488 It is also possible to split a function header over multiple lines, in between |
c001ee73519a
patch 8.2.0563: Vim9: cannot split a function line
Bram Moolenaar <Bram@vim.org>
parents:
20013
diff
changeset
|
489 arguments: > |
c001ee73519a
patch 8.2.0563: Vim9: cannot split a function line
Bram Moolenaar <Bram@vim.org>
parents:
20013
diff
changeset
|
490 def MyFunc( |
c001ee73519a
patch 8.2.0563: Vim9: cannot split a function line
Bram Moolenaar <Bram@vim.org>
parents:
20013
diff
changeset
|
491 text: string, |
c001ee73519a
patch 8.2.0563: Vim9: cannot split a function line
Bram Moolenaar <Bram@vim.org>
parents:
20013
diff
changeset
|
492 separator = '-' |
c001ee73519a
patch 8.2.0563: Vim9: cannot split a function line
Bram Moolenaar <Bram@vim.org>
parents:
20013
diff
changeset
|
493 ): string |
c001ee73519a
patch 8.2.0563: Vim9: cannot split a function line
Bram Moolenaar <Bram@vim.org>
parents:
20013
diff
changeset
|
494 |
23305 | 495 Since a continuation line cannot be easily recognized the parsing of commands |
23318
c76240efdf02
patch 8.2.2204: Vim9: using -> both for method and lambda is confusing
Bram Moolenaar <Bram@vim.org>
parents:
23305
diff
changeset
|
496 has been made stricter. E.g., because of the error in the first line, the |
23305 | 497 second line is seen as a separate command: > |
498 popup_create(some invalid expression, { | |
499 exit_cb: Func}) | |
500 Now "exit_cb: Func})" is actually a valid command: save any changes to the | |
501 file "_cb: Func})" and exit. To avoid this kind of mistake in Vim9 script | |
502 there must be white space between most command names and the argument. | |
503 | |
23666 | 504 However, the argument of a command that is a command won't be recognized. For |
505 example, after "windo echo expr" a line break inside "expr" will not be seen. | |
506 | |
23305 | 507 |
21250 | 508 Notes: |
509 - "enddef" cannot be used at the start of a continuation line, it ends the | |
510 current function. | |
511 - No line break is allowed in the LHS of an assignment. Specifically when | |
512 unpacking a list |:let-unpack|. This is OK: > | |
22171 | 513 [var1, var2] = |
21250 | 514 Func() |
515 < This does not work: > | |
22171 | 516 [var1, |
21250 | 517 var2] = |
518 Func() | |
519 - No line break is allowed in between arguments of an `:echo`, `:execute` and | |
520 similar commands. This is OK: > | |
22171 | 521 echo [1, |
21250 | 522 2] [3, |
523 4] | |
524 < This does not work: > | |
22171 | 525 echo [1, 2] |
21250 | 526 [3, 4] |
19999
844c7646f61b
patch 8.2.0555: Vim9: line continuation is not always needed
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
527 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
528 No curly braces expansion ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
529 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
530 |curly-braces-names| cannot be used. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
531 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
532 |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
533 Dictionary literals ~ |
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
534 |
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
535 Traditionally Vim has supported dictionary literals with a {} syntax: > |
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
536 let dict = {'key': value} |
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
537 |
23088
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
538 Later it became clear that using a simple text key is very common, thus |
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
539 literal dictionaries were introduced in a backwards compatible way: > |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
540 let dict = #{key: value} |
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
541 |
23088
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
542 However, this #{} syntax is unlike any existing language. As it turns out |
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
543 that using a literal key is much more common than using an expression, and |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
544 considering that JavaScript uses this syntax, using the {} form for dictionary |
23088
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
545 literals is considered a much more useful syntax. In Vim9 script the {} form |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
546 uses literal keys: > |
23666 | 547 var dict = {key: value} |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
548 |
23088
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
549 This works for alphanumeric characters, underscore and dash. If you want to |
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
550 use another character, use a single or double quoted string: > |
23666 | 551 var dict = {'key with space': value} |
552 var dict = {"key\twith\ttabs": value} | |
553 var dict = {'': value} # empty key | |
23088
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
554 |
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
555 In case the key needs to be an expression, square brackets can be used, just |
285cde4b8d0e
patch 8.2.2090: Vim9: dict does not accept a key in quotes
Bram Moolenaar <Bram@vim.org>
parents:
23047
diff
changeset
|
556 like in JavaScript: > |
23666 | 557 var dict = {["key" .. nr]: value} |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
558 |
23827
7e0d8f1cae7d
patch 8.2.2455: Vim9: key type for literal dict and indexing is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
23737
diff
changeset
|
559 The key type can be string, number, bool or float. Other types result in an |
7e0d8f1cae7d
patch 8.2.2455: Vim9: key type for literal dict and indexing is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
23737
diff
changeset
|
560 error. A number can be given with and without the []: > |
7e0d8f1cae7d
patch 8.2.2455: Vim9: key type for literal dict and indexing is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
23737
diff
changeset
|
561 var dict = {123: 'without', [456]: 'with'} |
7e0d8f1cae7d
patch 8.2.2455: Vim9: key type for literal dict and indexing is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
23737
diff
changeset
|
562 echo dict |
7e0d8f1cae7d
patch 8.2.2455: Vim9: key type for literal dict and indexing is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
23737
diff
changeset
|
563 {'456': 'with', '123': 'without'} |
7e0d8f1cae7d
patch 8.2.2455: Vim9: key type for literal dict and indexing is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
23737
diff
changeset
|
564 |
22936
00b0275ffe7f
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Bram Moolenaar <Bram@vim.org>
parents:
22723
diff
changeset
|
565 |
23978
54b2aa1f0d42
patch 8.2.2531: Vim9: the :k command is obscure
Bram Moolenaar <Bram@vim.org>
parents:
23974
diff
changeset
|
566 No :xit, :t, :k, :append, :change or :insert ~ |
19303 | 567 |
21584
d0c76ce48326
patch 8.2.1342: Vim9: accidentally using "t" gives a confusing error
Bram Moolenaar <Bram@vim.org>
parents:
21550
diff
changeset
|
568 These commands are too easily confused with local variable names. |
d0c76ce48326
patch 8.2.1342: Vim9: accidentally using "t" gives a confusing error
Bram Moolenaar <Bram@vim.org>
parents:
21550
diff
changeset
|
569 Instead of `:x` or `:xit` you can use `:exit`. |
d0c76ce48326
patch 8.2.1342: Vim9: accidentally using "t" gives a confusing error
Bram Moolenaar <Bram@vim.org>
parents:
21550
diff
changeset
|
570 Instead of `:t` you can use `:copy`. |
23978
54b2aa1f0d42
patch 8.2.2531: Vim9: the :k command is obscure
Bram Moolenaar <Bram@vim.org>
parents:
23974
diff
changeset
|
571 Instead of `:k` you can use `:mark`. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
572 |
19303 | 573 |
574 Comparators ~ | |
575 | |
576 The 'ignorecase' option is not used for comparators that use strings. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
577 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
578 |
23666 | 579 For loop ~ |
580 | |
581 Legacy Vim script has some tricks to make a for loop over a list handle | |
582 deleting items at the current or previous item. In Vim9 script it just uses | |
583 the index, if items are deleted then items in the list will be skipped. | |
584 Example legacy script: > | |
585 let l = [1, 2, 3, 4] | |
586 for i in l | |
587 echo i | |
588 call remove(l, index(l, i)) | |
589 endfor | |
590 Would echo: | |
591 1 | |
592 2 | |
593 3 | |
594 4 | |
595 In compiled Vim9 script you get: | |
596 1 | |
597 3 | |
598 Generally, you should not change the list that is iterated over. Make a copy | |
599 first if needed. | |
600 | |
601 | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
602 White space ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
603 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
604 Vim9 script enforces proper use of white space. This is no longer allowed: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
605 var name=234 # Error! |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
606 var name= 234 # Error! |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
607 var name =234 # Error! |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
608 There must be white space before and after the "=": > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
609 var name = 234 # OK |
21250 | 610 White space must also be put before the # that starts a comment after a |
611 command: > | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
612 var name = 234# Error! |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
613 var name = 234 # OK |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
614 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
615 White space is required around most operators. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
616 |
23466 | 617 White space is required in a sublist (list slice) around the ":", except at |
618 the start and end: > | |
619 otherlist = mylist[v : count] # v:count has a different meaning | |
620 otherlist = mylist[:] # make a copy of the List | |
621 otherlist = mylist[v :] | |
622 otherlist = mylist[: v] | |
623 | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
624 White space is not allowed: |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
625 - Between a function name and the "(": > |
23666 | 626 Func (arg) # Error! |
627 Func | |
22171 | 628 \ (arg) # Error! |
23666 | 629 Func |
630 (arg) # Error! | |
631 Func(arg) # OK | |
632 Func( | |
633 arg) # OK | |
634 Func( | |
635 arg # OK | |
636 ) | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
637 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
638 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
639 Conditions and expressions ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
640 |
22500
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
641 Conditions and expressions are mostly working like they do in other languages. |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
642 Some values are different from legacy Vim script: |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
643 value legacy Vim script Vim9 script ~ |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
644 0 falsy falsy |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
645 1 truthy truthy |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
646 99 truthy Error! |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
647 "0" falsy Error! |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
648 "99" truthy Error! |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
649 "text" falsy Error! |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
650 |
22500
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
651 For the "??" operator and when using "!" then there is no error, every value |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
652 is either falsy or truthy. This is mostly like JavaScript, except that an |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
653 empty list and dict is falsy: |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
654 |
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
655 type truthy when ~ |
23466 | 656 bool true, v:true or 1 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
657 number non-zero |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
658 float non-zero |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
659 string non-empty |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
660 blob non-empty |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
661 list non-empty (different from JavaScript) |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
662 dictionary non-empty (different from JavaScript) |
19968 | 663 func when there is a function name |
23466 | 664 special true or v:true |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
665 job when not NULL |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
666 channel when not NULL |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
667 class when not NULL |
23466 | 668 object when not NULL (TODO: when isTrue() returns true) |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
669 |
22494
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
670 The boolean operators "||" and "&&" expect the values to be boolean, zero or |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
671 one: > |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
672 1 || false == true |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
673 0 || 1 == true |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
674 0 || false == false |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
675 1 && true == true |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
676 0 && 1 == false |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
677 8 || 0 Error! |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
678 'yes' && 0 Error! |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
679 [] || 99 Error! |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
680 |
22494
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
681 When using "!" for inverting, there is no error for using any type and the |
22500
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
682 result is a boolean. "!!" can be used to turn any value into boolean: > |
23573 | 683 !'yes' == false |
22500
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
684 !![] == false |
23573 | 685 !![1, 2, 3] == true |
22494
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
686 |
4c21f7f6f9e3
patch 8.2.1795: Vim9: operators && and || have a confusing result
Bram Moolenaar <Bram@vim.org>
parents:
22441
diff
changeset
|
687 When using "`.."` for string concatenation arguments of simple types are |
22500
ef8a3177edc1
patch 8.2.1798: Vim9: trinary operator condition is too permissive
Bram Moolenaar <Bram@vim.org>
parents:
22494
diff
changeset
|
688 always converted to string: > |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
689 'hello ' .. 123 == 'hello 123' |
23466 | 690 'hello ' .. v:true == 'hello true' |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
691 |
21771
fcf978444298
patch 8.2.1435: Vim9: always converting to string for ".." leads to mistakes
Bram Moolenaar <Bram@vim.org>
parents:
21717
diff
changeset
|
692 Simple types are string, float, special and bool. For other types |string()| |
fcf978444298
patch 8.2.1435: Vim9: always converting to string for ".." leads to mistakes
Bram Moolenaar <Bram@vim.org>
parents:
21717
diff
changeset
|
693 can be used. |
23497
2247a2ce3630
patch 8.2.2291: Vim9: cannot use "null" for v:null
Bram Moolenaar <Bram@vim.org>
parents:
23466
diff
changeset
|
694 *false* *true* *null* |
2247a2ce3630
patch 8.2.2291: Vim9: cannot use "null" for v:null
Bram Moolenaar <Bram@vim.org>
parents:
23466
diff
changeset
|
695 In Vim9 script one can use "true" for v:true, "false" for v:false and "null" |
2247a2ce3630
patch 8.2.2291: Vim9: cannot use "null" for v:null
Bram Moolenaar <Bram@vim.org>
parents:
23466
diff
changeset
|
696 for v:null. When converting a boolean to a string "false" and "true" are |
2247a2ce3630
patch 8.2.2291: Vim9: cannot use "null" for v:null
Bram Moolenaar <Bram@vim.org>
parents:
23466
diff
changeset
|
697 used, not "v:false" and "v:true" like in legacy script. "v:none" is not |
2247a2ce3630
patch 8.2.2291: Vim9: cannot use "null" for v:null
Bram Moolenaar <Bram@vim.org>
parents:
23466
diff
changeset
|
698 changed, it is only used in JSON and has no equivalent in other languages. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
699 |
24128
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
700 Indexing a string with [idx] or taking a slice with [idx : idx] uses character |
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
701 indexes instead of byte indexes. Composing characters are included. |
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
702 Example: > |
21825 | 703 echo 'bár'[1] |
704 In legacy script this results in the character 0xc3 (an illegal byte), in Vim9 | |
705 script this results in the string 'á'. | |
23573 | 706 A negative index is counting from the end, "[-1]" is the last character. |
23666 | 707 To exclude the last character use |slice()|. |
23573 | 708 If the index is out of range then an empty string results. |
709 | |
710 In legacy script "++var" and "--var" would be silently accepted and have no | |
711 effect. This is an error in Vim9 script. | |
712 | |
713 Numbers starting with zero are not considered to be octal, only numbers | |
714 starting with "0o" are octal: "0o744". |scriptversion-4| | |
21825 | 715 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
716 |
21093 | 717 What to watch out for ~ |
718 *vim9-gotchas* | |
719 Vim9 was designed to be closer to often used programming languages, but at the | |
720 same time tries to support the legacy Vim commands. Some compromises had to | |
721 be made. Here is a summary of what might be unexpected. | |
722 | |
723 Ex command ranges need to be prefixed with a colon. > | |
23666 | 724 -> legacy Vim: shifts the previous line to the right |
725 ->func() Vim9: method call in a continuation line | |
726 :-> Vim9: shifts the previous line to the right | |
21093 | 727 |
23666 | 728 %s/a/b legacy Vim: substitute on all lines |
21093 | 729 x = alongname |
23666 | 730 % another Vim9: modulo operator in a continuation line |
731 :%s/a/b Vim9: substitute on all lines | |
732 't legacy Vim: jump to mark t | |
733 'text'->func() Vim9: method call | |
734 :'t Vim9: jump to mark t | |
21093 | 735 |
21676 | 736 Some Ex commands can be confused with assignments in Vim9 script: > |
23666 | 737 g:name = value # assignment |
738 g:pattern:cmd # invalid command - ERROR | |
739 :g:pattern:cmd # :global command | |
21676 | 740 |
21093 | 741 Functions defined with `:def` compile the whole function. Legacy functions |
742 can bail out, and the following lines are not parsed: > | |
743 func Maybe() | |
744 if !has('feature') | |
745 return | |
746 endif | |
747 use-feature | |
748 endfunc | |
749 Vim9 functions are compiled as a whole: > | |
750 def Maybe() | |
751 if !has('feature') | |
752 return | |
753 endif | |
23573 | 754 use-feature # May give a compilation error |
21093 | 755 enddef |
756 For a workaround, split it in two functions: > | |
757 func Maybe() | |
758 if has('feature') | |
23666 | 759 call MaybeInner() |
21093 | 760 endif |
761 endfunc | |
762 if has('feature') | |
763 def MaybeInner() | |
764 use-feature | |
765 enddef | |
766 endif | |
22171 | 767 Or put the unsupported code inside an `if` with a constant expression that |
21991 | 768 evaluates to false: > |
769 def Maybe() | |
770 if has('feature') | |
771 use-feature | |
772 endif | |
773 enddef | |
23573 | 774 < *vim9-user-command* |
23666 | 775 Another side effect of compiling a function is that the presence of a user |
23573 | 776 command is checked at compile time. If the user command is defined later an |
777 error will result. This works: > | |
778 command -nargs=1 MyCommand echom <q-args> | |
779 def Works() | |
780 MyCommand 123 | |
781 enddef | |
782 This will give an error for "MyCommand" not being defined: > | |
783 def Works() | |
784 command -nargs=1 MyCommand echom <q-args> | |
785 MyCommand 123 | |
786 enddef | |
787 A workaround is to invoke the command indirectly with `:execute`: > | |
788 def Works() | |
789 command -nargs=1 MyCommand echom <q-args> | |
790 execute 'MyCommand 123' | |
791 enddef | |
792 | |
21991 | 793 Note that for unrecognized commands there is no check for "|" and a following |
794 command. This will give an error for missing `endif`: > | |
795 def Maybe() | |
796 if has('feature') | use-feature | endif | |
797 enddef | |
21093 | 798 |
23305 | 799 Other differences ~ |
800 | |
801 Patterns are used like 'magic' is set, unless explicitly overruled. | |
802 The 'edcompatible' option value is not used. | |
803 The 'gdefault' option value is not used. | |
804 | |
24103 | 805 You may also find this wiki useful. It was written by an early adoptor of |
806 Vim9 script: https://github.com/lacygoill/wiki/blob/master/vim/vim9.md | |
23305 | 807 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
808 ============================================================================== |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
809 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
810 3. New style functions *fast-functions* |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
811 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
812 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
813 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
814 *:def* |
21825 | 815 :def[!] {name}([arguments])[: {return-type}] |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
816 Define a new function by the name {name}. The body of |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
817 the function follows in the next lines, until the |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
818 matching `:enddef`. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
819 |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
820 When {return-type} is omitted or is "void" the |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
821 function is not expected to return anything. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
822 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
823 {arguments} is a sequence of zero or more argument |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
824 declarations. There are three forms: |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
825 {name}: {type} |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
826 {name} = {value} |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
827 {name}: {type} = {value} |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
828 The first form is a mandatory argument, the caller |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
829 must always provide them. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
830 The second and third form are optional arguments. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
831 When the caller omits an argument the {value} is used. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
832 |
20856 | 833 The function will be compiled into instructions when |
21499 | 834 called, or when `:disassemble` or `:defcompile` is |
835 used. Syntax and type errors will be produced at that | |
836 time. | |
20856 | 837 |
21499 | 838 It is possible to nest `:def` inside another `:def` or |
839 `:function` up to about 50 levels deep. | |
19303 | 840 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
841 [!] is used as with `:function`. Note that |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
842 script-local functions cannot be deleted or redefined |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
843 later in Vim9 script. They can only be removed by |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
844 reloading the same script. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
845 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
846 *:enddef* |
21499 | 847 :enddef End of a function defined with `:def`. It should be on |
848 a line by its own. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
849 |
24128
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
850 You may also find this wiki useful. It was written by an early adoptor of |
fcbb1d4df15b
patch 8.2.2605: Vim9: string index and slice does not include composing chars
Bram Moolenaar <Bram@vim.org>
parents:
24103
diff
changeset
|
851 Vim9 script: https://github.com/lacygoill/wiki/blob/master/vim/vim9.md |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
852 |
19473
b09afbebffee
patch 8.2.0294: cannot use Ex command that is also a function name
Bram Moolenaar <Bram@vim.org>
parents:
19404
diff
changeset
|
853 If the script the function is defined in is Vim9 script, then script-local |
b09afbebffee
patch 8.2.0294: cannot use Ex command that is also a function name
Bram Moolenaar <Bram@vim.org>
parents:
19404
diff
changeset
|
854 variables can be accessed without the "s:" prefix. They must be defined |
20856 | 855 before the function is compiled. If the script the function is defined in is |
856 legacy script, then script-local variables must be accessed with the "s:" | |
21991 | 857 prefix and they do not need to exist (they can be deleted any time). |
19473
b09afbebffee
patch 8.2.0294: cannot use Ex command that is also a function name
Bram Moolenaar <Bram@vim.org>
parents:
19404
diff
changeset
|
858 |
20552 | 859 *:defc* *:defcompile* |
860 :defc[ompile] Compile functions defined in the current script that | |
861 were not compiled yet. | |
862 This will report errors found during the compilation. | |
19473
b09afbebffee
patch 8.2.0294: cannot use Ex command that is also a function name
Bram Moolenaar <Bram@vim.org>
parents:
19404
diff
changeset
|
863 |
19404 | 864 *:disa* *:disassemble* |
865 :disa[ssemble] {func} Show the instructions generated for {func}. | |
866 This is for debugging and testing. | |
19556
ff5048b0ccfe
patch 8.2.0335: no completion for :disassemble
Bram Moolenaar <Bram@vim.org>
parents:
19523
diff
changeset
|
867 Note that for command line completion of {func} you |
ff5048b0ccfe
patch 8.2.0335: no completion for :disassemble
Bram Moolenaar <Bram@vim.org>
parents:
19523
diff
changeset
|
868 can prepend "s:" to find script-local functions. |
19404 | 869 |
23737 | 870 :disa[ssemble]! {func} Like `:disassemble` but with the instructions used for |
871 profiling. | |
872 | |
21250 | 873 Limitations ~ |
874 | |
875 Local variables will not be visible to string evaluation. For example: > | |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
876 def MapList(): list<string> |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
877 var list = ['aa', 'bb', 'cc', 'dd'] |
21250 | 878 return range(1, 2)->map('list[v:val]') |
879 enddef | |
880 | |
881 The map argument is a string expression, which is evaluated without the | |
882 function scope. Instead, use a lambda: > | |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
883 def MapList(): list<string> |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
884 var list = ['aa', 'bb', 'cc', 'dd'] |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
885 return range(1, 2)->map(( _, v) => list[v]) |
21250 | 886 enddef |
887 | |
23358
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
888 The same is true for commands that are not compiled, such as `:global`. |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
889 For these the backtick expansion can be used. Example: > |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
890 def Replace() |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
891 var newText = 'blah' |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
892 g/pattern/s/^/`=newText`/ |
b3142fc0a414
patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents:
23318
diff
changeset
|
893 enddef |
21250 | 894 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
895 ============================================================================== |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
896 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
897 4. Types *vim9-types* |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
898 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
899 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
900 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
901 The following builtin types are supported: |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
902 bool |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
903 number |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
904 float |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
905 string |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
906 blob |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
907 list<{type}> |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
908 dict<{type}> |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
909 job |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
910 channel |
19646 | 911 func |
19968 | 912 func: {type} |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
913 func({type}, ...) |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
914 func({type}, ...): {type} |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
915 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
916 Not supported yet: |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
917 tuple<a: {type}, b: {type}, ...> |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
918 |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
919 These types can be used in declarations, but no value will have this type: |
21499 | 920 {type}|{type} {not implemented yet} |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
921 void |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
922 any |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
923 |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
924 There is no array type, use list<{type}> instead. For a list constant an |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
925 efficient implementation is used that avoids allocating lot of small pieces of |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
926 memory. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
927 |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
928 A partial and function can be declared in more or less specific ways: |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
929 func any kind of function reference, no type |
19968 | 930 checking for arguments or return value |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
931 func: {type} any number and type of arguments with specific |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
932 return type |
19968 | 933 func({type}) function with argument type, does not return |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
934 a value |
19968 | 935 func({type}): {type} function with argument type and return type |
936 func(?{type}) function with type of optional argument, does | |
937 not return a value | |
938 func(...{type}) function with type of variable number of | |
939 arguments, does not return a value | |
940 func({type}, ?{type}, ...{type}): {type} | |
941 function with: | |
942 - type of mandatory argument | |
943 - type of optional argument | |
944 - type of variable number of arguments | |
945 - return type | |
19904
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
946 |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
947 If the return type is "void" the function does not return a value. |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
948 |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
949 The reference can also be a |Partial|, in which case it stores extra arguments |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
950 and/or a dictionary, which are not visible to the caller. Since they are |
bd4f91762d0f
patch 8.2.0508: Vim9: func and partial types not done yet
Bram Moolenaar <Bram@vim.org>
parents:
19646
diff
changeset
|
951 called in the same way the declaration is the same. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
952 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
953 Custom types can be defined with `:type`: > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
954 :type MyList list<string> |
21715
571832713efa
patch 8.2.1407: Vim9: type of list and dict only depends on first item
Bram Moolenaar <Bram@vim.org>
parents:
21676
diff
changeset
|
955 Custom types must start with a capital letter, to avoid name clashes with |
571832713efa
patch 8.2.1407: Vim9: type of list and dict only depends on first item
Bram Moolenaar <Bram@vim.org>
parents:
21676
diff
changeset
|
956 builtin types added later, similarly to user functions. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
957 {not implemented yet} |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
958 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
959 And classes and interfaces can be used as types: > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
960 :class MyClass |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
961 :var mine: MyClass |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
962 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
963 :interface MyInterface |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
964 :var mine: MyInterface |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
965 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
966 :class MyTemplate<Targ> |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
967 :var mine: MyTemplate<number> |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
968 :var mine: MyTemplate<string> |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
969 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
970 :class MyInterface<Targ> |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
971 :var mine: MyInterface<number> |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
972 :var mine: MyInterface<string> |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
973 {not implemented yet} |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
974 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
975 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
976 Variable types and type casting ~ |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
977 *variable-types* |
21717
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
978 Variables declared in Vim9 script or in a `:def` function have a type, either |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
979 specified explicitly or inferred from the initialization. |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
980 |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
981 Global, buffer, window and tab page variables do not have a specific type, the |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
982 value can be changed at any time, possibly changing the type. Therefore, in |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
983 compiled code the "any" type is assumed. |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
984 |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
985 This can be a problem when the "any" type is undesired and the actual type is |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
986 expected to always be the same. For example, when declaring a list: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
987 var l: list<number> = [1, g:two] |
23305 | 988 At compile time Vim doesn't know the type of "g:two" and the expression type |
989 becomes list<any>. An instruction is generated to check the list type before | |
990 doing the assignment, which is a bit inefficient. | |
991 *type-casting* | |
992 To avoid this, use a type cast: > | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
993 var l: list<number> = [1, <number>g:two] |
23305 | 994 The compiled code will then only check that "g:two" is a number and give an |
995 error if it isn't. This is called type casting. | |
21717
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
996 |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
997 The syntax of a type cast is: "<" {type} ">". There cannot be white space |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
998 after the "<" or before the ">" (to avoid them being confused with |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
999 smaller-than and bigger-than operators). |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
1000 |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
1001 The semantics is that, if needed, a runtime type check is performed. The |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
1002 value is not actually changed. If you need to change the type, e.g. to change |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
1003 it to a string, use the |string()| function. Or use |str2nr()| to convert a |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
1004 string to a number. |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
1005 |
ef3b31d510d2
patch 8.2.1408: Vim9: type casting not supported
Bram Moolenaar <Bram@vim.org>
parents:
21715
diff
changeset
|
1006 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1007 Type inference ~ |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1008 *type-inference* |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1009 In general: Whenever the type is clear it can be omitted. For example, when |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1010 declaring a variable and giving it a value: > |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1011 var name = 0 # infers number type |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1012 var name = 'hello' # infers string type |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1013 |
21715
571832713efa
patch 8.2.1407: Vim9: type of list and dict only depends on first item
Bram Moolenaar <Bram@vim.org>
parents:
21676
diff
changeset
|
1014 The type of a list and dictionary comes from the common type of the values. |
571832713efa
patch 8.2.1407: Vim9: type of list and dict only depends on first item
Bram Moolenaar <Bram@vim.org>
parents:
21676
diff
changeset
|
1015 If the values all have the same type, that type is used for the list or |
571832713efa
patch 8.2.1407: Vim9: type of list and dict only depends on first item
Bram Moolenaar <Bram@vim.org>
parents:
21676
diff
changeset
|
1016 dictionary. If there is a mix of types, the "any" type is used. > |
571832713efa
patch 8.2.1407: Vim9: type of list and dict only depends on first item
Bram Moolenaar <Bram@vim.org>
parents:
21676
diff
changeset
|
1017 [1, 2, 3] list<number> |
571832713efa
patch 8.2.1407: Vim9: type of list and dict only depends on first item
Bram Moolenaar <Bram@vim.org>
parents:
21676
diff
changeset
|
1018 ['a', 'b', 'c'] list<string> |
571832713efa
patch 8.2.1407: Vim9: type of list and dict only depends on first item
Bram Moolenaar <Bram@vim.org>
parents:
21676
diff
changeset
|
1019 [1, 'x', 3] list<any> |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1020 |
21991 | 1021 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1022 Stricter type checking ~ |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1023 *type-checking* |
21991 | 1024 In legacy Vim script, where a number was expected, a string would be |
1025 automatically converted to a number. This was convenient for an actual number | |
1026 such as "123", but leads to unexpected problems (but no error message) if the | |
1027 string doesn't start with a number. Quite often this leads to hard-to-find | |
1028 bugs. | |
1029 | |
1030 In Vim9 script this has been made stricter. In most places it works just as | |
22171 | 1031 before, if the value used matches the expected type. There will sometimes be |
1032 an error, thus breaking backwards compatibility. For example: | |
21991 | 1033 - Using a number other than 0 or 1 where a boolean is expected. *E1023* |
1034 - Using a string value when setting a number options. | |
1035 - Using a number where a string is expected. *E1024* | |
1036 | |
23573 | 1037 One consequence is that the item type of a list or dict given to map() must |
24024 | 1038 not change. This will give an error in Vim9 script: > |
23573 | 1039 map([1, 2, 3], (i, v) => 'item ' .. i) |
24024 | 1040 E1012: Type mismatch; expected number but got string |
1041 Instead use |mapnew()|. If the item type was determined to be "any" it can | |
1042 change to a more specific type. E.g. when a list of mixed types gets changed | |
1043 to a list of numbers. | |
1044 Same for |extend()|, use |extendnew()| instead, and for |flatten()|, use | |
1045 |flattennew()| instead. | |
23573 | 1046 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1047 ============================================================================== |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1048 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1049 5. Namespace, Import and Export |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1050 *vim9script* *vim9-export* *vim9-import* |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1051 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1052 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1053 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1054 A Vim9 script can be written to be imported. This means that everything in |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1055 the script is local, unless exported. Those exported items, and only those |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1056 items, can then be imported in another script. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1057 |
21991 | 1058 You can cheat by using the global namespace explicitly. We will assume here |
1059 that you don't do that. | |
1060 | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1061 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1062 Namespace ~ |
23392
517fca70e084
patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents:
23360
diff
changeset
|
1063 *vim9-namespace* |
19303 | 1064 To recognize a file that can be imported the `vim9script` statement must |
23974
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1065 appear as the first statement in the file (see |vim9-mix| for an exception). |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1066 It tells Vim to interpret the script in its own namespace, instead of the |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1067 global namespace. If a file starts with: > |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1068 vim9script |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1069 var myvar = 'yes' |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1070 Then "myvar" will only exist in this file. While without `vim9script` it would |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1071 be available as `g:myvar` from any other script and function. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1072 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1073 The variables at the file level are very much like the script-local "s:" |
20115 | 1074 variables in legacy Vim script, but the "s:" is omitted. And they cannot be |
1075 deleted. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1076 |
20115 | 1077 In Vim9 script the global "g:" namespace can still be used as before. And the |
1078 "w:", "b:" and "t:" namespaces. These have in common that variables are not | |
1079 declared and they can be deleted. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1080 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1081 A side effect of `:vim9script` is that the 'cpoptions' option is set to the |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1082 Vim default value, like with: > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1083 :set cpo&vim |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1084 One of the effects is that |line-continuation| is always enabled. |
24150
4919f2d8d7fd
patch 8.2.2616: Vim9: if 'cpo' is change in Vim9 script it may be restored
Bram Moolenaar <Bram@vim.org>
parents:
24128
diff
changeset
|
1085 The original value of 'cpoptions' is restored at the end of the script, while |
4919f2d8d7fd
patch 8.2.2616: Vim9: if 'cpo' is change in Vim9 script it may be restored
Bram Moolenaar <Bram@vim.org>
parents:
24128
diff
changeset
|
1086 flags added or removed in the script are also added to or removed from the |
4919f2d8d7fd
patch 8.2.2616: Vim9: if 'cpo' is change in Vim9 script it may be restored
Bram Moolenaar <Bram@vim.org>
parents:
24128
diff
changeset
|
1087 original value to get the same effect. The order of flags may change. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1088 |
23974
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1089 *vim9-mix* |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1090 There is one way to use both legacy and Vim9 syntax in one script file: > |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1091 " comments may go here |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1092 if !has('vim9script') |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1093 " legacy script commands go here |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1094 finish |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1095 endif |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1096 vim9script |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1097 # Vim9 script commands go here |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1098 This allows for writing a script that takes advantage of the Vim9 script |
24024 | 1099 syntax if possible, but will also work on a Vim version without it. |
23974
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1100 |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1101 This can only work in two ways: |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1102 1. The "if" statement evaluates to false, the commands up to `endif` are |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1103 skipped and `vim9script` is then the first command actually executed. |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1104 2. The "if" statement evaluates to true, the commands up to `endif` are |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1105 executed and `finish` bails out before reaching `vim9script`. |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1106 |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1107 TODO: The "vim9script" feature does not exist yet, it will only be added once |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1108 the Vim9 script syntax has been fully implemented. |
d4f7e4138544
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Bram Moolenaar <Bram@vim.org>
parents:
23938
diff
changeset
|
1109 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1110 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1111 Export ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1112 *:export* *:exp* |
21499 | 1113 Exporting an item can be written as: > |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1114 export const EXPORTED_CONST = 1234 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1115 export var someValue = ... |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1116 export final someValue = ... |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1117 export const someValue = ... |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1118 export def MyFunc() ... |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1119 export class MyClass ... |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1120 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1121 As this suggests, only constants, variables, `:def` functions and classes can |
24103 | 1122 be exported. {not implemented yet: export class} |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1123 |
20856 | 1124 *E1042* |
1125 `:export` can only be used in Vim9 script, at the script level. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1126 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1127 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1128 Import ~ |
20965 | 1129 *:import* *:imp* *E1094* |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1130 The exported items can be imported individually in another Vim9 script: > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1131 import EXPORTED_CONST from "thatscript.vim" |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1132 import MyClass from "myclass.vim" |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1133 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1134 To import multiple items at the same time: > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1135 import {someValue, MyClass} from "thatscript.vim" |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1136 |
19303 | 1137 In case the name is ambiguous, another name can be specified: > |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1138 import MyClass as ThatClass from "myclass.vim" |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1139 import {someValue, MyClass as ThatClass} from "myclass.vim" |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1140 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1141 To import all exported items under a specific identifier: > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1142 import * as That from 'thatscript.vim' |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1143 |
22328 | 1144 {not implemented yet: using "This as That"} |
1145 | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1146 Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1147 to choose the name "That", but it is highly recommended to use the name of the |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1148 script file to avoid confusion. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1149 |
22171 | 1150 `:import` can also be used in legacy Vim script. The imported items still |
1151 become script-local, even when the "s:" prefix is not given. | |
1152 | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1153 The script name after `import` can be: |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1154 - A relative path, starting "." or "..". This finds a file relative to the |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1155 location of the script file itself. This is useful to split up a large |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1156 plugin into several files. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1157 - An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This |
22723 | 1158 will rarely be used. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1159 - A path not being relative or absolute. This will be found in the |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1160 "import" subdirectories of 'runtimepath' entries. The name will usually be |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1161 longer and unique, to avoid loading the wrong file. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1162 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1163 Once a vim9 script file has been imported, the result is cached and used the |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1164 next time the same script is imported. It will not be read again. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1165 *:import-cycle* |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1166 The `import` commands are executed when encountered. If that script (directly |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1167 or indirectly) imports the current script, then items defined after the |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1168 `import` won't be processed yet. Therefore cyclic imports can exist, but may |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1169 result in undefined items. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1170 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1171 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1172 Import in an autoload script ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1173 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1174 For optimal startup speed, loading scripts should be postponed until they are |
19303 | 1175 actually needed. A recommended mechanism: |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1176 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1177 1. In the plugin define user commands, functions and/or mappings that refer to |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1178 an autoload script. > |
23666 | 1179 command -nargs=1 SearchForStuff searchfor#Stuff(<f-args>) |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1180 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1181 < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1182 |
21825 | 1183 2. In the autoload script do the actual work. You can import items from |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1184 other files to split up functionality in appropriate pieces. > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1185 vim9script |
23573 | 1186 import FilterFunc from "../import/someother.vim" |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1187 def searchfor#Stuff(arg: string) |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1188 var filtered = FilterFunc(arg) |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1189 ... |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1190 < This goes in .../autoload/searchfor.vim. "searchfor" in the file name |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1191 must be exactly the same as the prefix for the function name, that is how |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1192 Vim finds the file. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1193 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1194 3. Other functionality, possibly shared between plugins, contains the exported |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1195 items and any private items. > |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1196 vim9script |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1197 var localVar = 'local' |
22171 | 1198 export def FilterFunc(arg: string): string |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1199 ... |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1200 < This goes in .../import/someother.vim. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1201 |
21771
fcf978444298
patch 8.2.1435: Vim9: always converting to string for ".." leads to mistakes
Bram Moolenaar <Bram@vim.org>
parents:
21717
diff
changeset
|
1202 When compiling a `:def` function and a function in an autoload script is |
fcf978444298
patch 8.2.1435: Vim9: always converting to string for ".." leads to mistakes
Bram Moolenaar <Bram@vim.org>
parents:
21717
diff
changeset
|
1203 encountered, the script is not loaded until the `:def` function is called. |
fcf978444298
patch 8.2.1435: Vim9: always converting to string for ".." leads to mistakes
Bram Moolenaar <Bram@vim.org>
parents:
21717
diff
changeset
|
1204 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1205 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1206 Import in legacy Vim script ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1207 |
20856 | 1208 If an `import` statement is used in legacy Vim script, the script-local "s:" |
1209 namespace will be used for the imported item, even when "s:" is not specified. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1210 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1211 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1212 ============================================================================== |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1213 |
22328 | 1214 6. Future work: classes *vim9-classes* |
1215 | |
1216 Above "class" was mentioned a few times, but it has not been implemented yet. | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1217 Most of Vim9 script can be created without this functionality, and since |
22328 | 1218 implementing classes is going to be a lot of work, it is left for the future. |
1219 For now we'll just make sure classes can be added later. | |
1220 | |
1221 Thoughts: | |
1222 - `class` / `endclass`, everything in one file | |
1223 - Class names are always CamelCase | |
1224 - Single constructor | |
1225 - Single inheritance with `class ThisClass extends BaseClass` | |
1226 - `abstract class` | |
1227 - `interface` (Abstract class without any implementation) | |
1228 - `class SomeClass implements SomeInterface` | |
1229 - Generics for class: `class <Tkey, Tentry>` | |
1230 - Generics for function: `def <Tkey> GetLast(key: Tkey)` | |
1231 | |
1232 Again, much of this is from TypeScript. | |
1233 | |
1234 Some things that look like good additions: | |
1235 - Use a class as an interface (like Dart) | |
1236 - Extend a class with methods, using an import (like Dart) | |
1237 | |
1238 An important class that will be provided is "Promise". Since Vim is single | |
1239 threaded, connecting asynchronous operations is a natural way of allowing | |
1240 plugins to do their work without blocking the user. It's a uniform way to | |
1241 invoke callbacks and handle timeouts and errors. | |
1242 | |
1243 ============================================================================== | |
1244 | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1245 9. Rationale *vim9-rationale* |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1246 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1247 The :def command ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1248 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1249 Plugin writers have asked for much faster Vim script. Investigations have |
19303 | 1250 shown that keeping the existing semantics of function calls make this close to |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1251 impossible, because of the overhead involved with calling a function, setting |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1252 up the local function scope and executing lines. There are many details that |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1253 need to be handled, such as error messages and exceptions. The need to create |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1254 a dictionary for a: and l: scopes, the a:000 list and several others add too |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1255 much overhead that cannot be avoided. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1256 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1257 Therefore the `:def` method to define a new-style function had to be added, |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1258 which allows for a function with different semantics. Most things still work |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1259 as before, but some parts do not. A new way to define a function was |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1260 considered the best way to separate the legacy style code from Vim9 style code. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1261 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1262 Using "def" to define a function comes from Python. Other languages use |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1263 "function" which clashes with legacy Vim script. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1264 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1265 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1266 Type checking ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1267 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1268 When compiling lines of Vim commands into instructions as much as possible |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1269 should be done at compile time. Postponing it to runtime makes the execution |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1270 slower and means mistakes are found only later. For example, when |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1271 encountering the "+" character and compiling this into a generic add |
23666 | 1272 instruction, at runtime the instruction would have to inspect the type of the |
1273 arguments and decide what kind of addition to do. And when the type is | |
1274 dictionary throw an error. If the types are known to be numbers then an "add | |
1275 number" instruction can be used, which is faster. The error can be given at | |
1276 compile time, no error handling is needed at runtime, since adding two numbers | |
1277 cannot fail. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1278 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1279 The syntax for types, using <type> for compound types, is similar to Java. It |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1280 is easy to understand and widely used. The type names are what were used in |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1281 Vim before, with some additions such as "void" and "bool". |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1282 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1283 |
22328 | 1284 Removing clutter and weirdness ~ |
20856 | 1285 |
22328 | 1286 Once decided that `:def` functions have different syntax than legacy functions, |
1287 we are free to add improvements to make the code more familiar for users who | |
1288 know popular programming languages. In other words: remove weird things that | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1289 only Vim does. |
22328 | 1290 |
1291 We can also remove clutter, mainly things that were done to make Vim script | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1292 backwards compatible with the good old Vi commands. |
20856 | 1293 |
22328 | 1294 Examples: |
1295 - Drop `:call` for calling a function and `:eval` for manipulating data. | |
1296 - Drop using a leading backslash for line continuation, automatically figure | |
1297 out where an expression ends. | |
20856 | 1298 |
22328 | 1299 However, this does require that some things need to change: |
1300 - Comments start with # instead of ", to avoid confusing them with strings. | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1301 This is good anyway, it is known from several popular languages. |
22328 | 1302 - Ex command ranges need to be prefixed with a colon, to avoid confusion with |
1303 expressions (single quote can be a string or a mark, "/" can be divide or a | |
1304 search command, etc.). | |
1305 | |
1306 Goal is to limit the differences. A good criteria is that when the old syntax | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1307 is accidentally used you are very likely to get an error message. |
20856 | 1308 |
1309 | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1310 Syntax and semantics from popular languages ~ |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1311 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1312 Script writers have complained that the Vim script syntax is unexpectedly |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1313 different from what they are used to. To reduce this complaint popular |
20856 | 1314 languages are used as an example. At the same time, we do not want to abandon |
1315 the well-known parts of legacy Vim script. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1316 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1317 For many things TypeScript is followed. It's a recent language that is |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1318 gaining popularity and has similarities with Vim script. It also has a |
22328 | 1319 mix of static typing (a variable always has a known value type) and dynamic |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1320 typing (a variable can have different types, this changes at runtime). Since |
22328 | 1321 legacy Vim script is dynamically typed and a lot of existing functionality |
1322 (esp. builtin functions) depends on that, while static typing allows for much | |
1323 faster execution, we need to have this mix in Vim9 script. | |
1324 | |
22171 | 1325 There is no intention to completely match TypeScript syntax and semantics. We |
1326 just want to take those parts that we can use for Vim and we expect Vim users | |
22328 | 1327 will be happy with. TypeScript is a complex language with its own history, |
1328 advantages and disadvantages. To get an idea of the disadvantages read the | |
1329 book: "JavaScript: The Good Parts". Or find the article "TypeScript: the good | |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1330 parts" and read the "Things to avoid" section. |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1331 |
22328 | 1332 People familiar with other languages (Java, Python, etc.) will also find |
1333 things in TypeScript that they do not like or do not understand. We'll try to | |
1334 avoid those things. | |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1335 |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1336 Specific items from TypeScript we avoid: |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1337 - Overloading "+", using it both for addition and string concatenation. This |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1338 goes against legacy Vim script and often leads to mistakes. For that reason |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1339 we will keep using ".." for string concatenation. Lua also uses ".." this |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1340 way. And it allows for conversion to string for more values. |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1341 - TypeScript can use an expression like "99 || 'yes'" in a condition, but |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1342 cannot assign the value to a boolean. That is inconsistent and can be |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1343 annoying. Vim recognizes an expression with && or || and allows using the |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1344 result as a bool. TODO: to be reconsidered |
22272
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1345 - TypeScript considers an empty string as Falsy, but an empty list or dict as |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1346 Truthy. That is inconsistent. In Vim an empty list and dict are also |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1347 Falsy. |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1348 - TypeScript has various "Readonly" types, which have limited usefulness, |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1349 since a type cast can remove the immutable nature. Vim locks the value, |
eb1f5f618c75
patch 8.2.1685: Vim9: cannot declare a constant value
Bram Moolenaar <Bram@vim.org>
parents:
22171
diff
changeset
|
1350 which is more flexible, but is only checked at runtime. |
22171 | 1351 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1352 |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1353 Declarations ~ |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1354 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1355 Legacy Vim script uses `:let` for every assignment, while in Vim9 declarations |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1356 are used. That is different, thus it's good to use a different command: |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1357 `:var`. This is used in many languages. The semantics might be slightly |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1358 different, but it's easily recognized as a declaration. |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1359 |
23047 | 1360 Using `:const` for constants is common, but the semantics varies. Some |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1361 languages only make the variable immutable, others also make the value |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1362 immutable. Since "final" is well known from Java for only making the variable |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1363 immutable we decided to use that. And then `:const` can be used for making |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1364 both immutable. This was also used in legacy Vim script and the meaning is |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1365 almost the same. |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1366 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1367 What we end up with is very similar to Dart: > |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1368 :var name # mutable variable and value |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1369 :final name # immutable variable, mutable value |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1370 :const name # immutable variable and value |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1371 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1372 Since legacy and Vim9 script will be mixed and global variables will be |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1373 shared, optional type checking is desirable. Also, type inference will avoid |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1374 the need for specifying the type in many cases. The TypeScript syntax fits |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1375 best for adding types to declarations: > |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1376 var name: string # string type is specified |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1377 ... |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1378 name = 'John' |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1379 const greeting = 'hello' # string type is inferred |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1380 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1381 This is how we put types in a declaration: > |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1382 var mylist: list<string> |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1383 final mylist: list<string> = ['foo'] |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1384 def Func(arg1: number, arg2: string): bool |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1385 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1386 Two alternatives were considered: |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1387 1. Put the type before the name, like Dart: > |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1388 var list<string> mylist |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1389 final list<string> mylist = ['foo'] |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1390 def Func(number arg1, string arg2) bool |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1391 2. Put the type after the variable name, but do not use a colon, like Go: > |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1392 var mylist list<string> |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1393 final mylist list<string> = ['foo'] |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1394 def Func(arg1 number, arg2 string) bool |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1395 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1396 The first is more familiar for anyone used to C or Java. The second one |
22565 | 1397 doesn't really have an advantage over the first, so let's discard the second. |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1398 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1399 Since we use type inference the type can be left out when it can be inferred |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1400 from the value. This means that after `var` we don't know if a type or a name |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1401 follows. That makes parsing harder, not only for Vim but also for humans. |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1402 Also, it will not be allowed to use a variable name that could be a type name, |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1403 using `var string string` is too confusing. |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1404 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1405 The chosen syntax, using a colon to separate the name from the type, adds |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1406 punctuation, but it actually makes it easier to recognize the parts of a |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1407 declaration. |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1408 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1409 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1410 Expressions ~ |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1411 |
22565 | 1412 Expression evaluation was already close to what other languages are doing. |
1413 Some details are unexpected and can be improved. For example a boolean | |
1414 condition would accept a string, convert it to a number and check if the | |
1415 number is non-zero. This is unexpected and often leads to mistakes, since | |
1416 text not starting with a number would be converted to zero, which is | |
22723 | 1417 considered false. Thus using a string for a condition would often not give an |
1418 error and be considered false. That is confusing. | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1419 |
23047 | 1420 In Vim9 type checking is stricter to avoid mistakes. Where a condition is |
22565 | 1421 used, e.g. with the `:if` command and the `||` operator, only boolean-like |
1422 values are accepted: | |
1423 true: `true`, `v:true`, `1`, `0 < 9` | |
1424 false: `false`, `v:false`, `0`, `0 > 9` | |
1425 Note that the number zero is false and the number one is true. This is more | |
22723 | 1426 permissive than most other languages. It was done because many builtin |
22565 | 1427 functions return these values. |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1428 |
22565 | 1429 If you have any type of value and want to use it as a boolean, use the `!!` |
1430 operator: | |
1431 true: !`!'text'`, `!![99]`, `!!{'x': 1}`, `!!99` | |
1432 false: `!!''`, `!![]`, `!!{}` | |
1433 | |
1434 From a language like JavaScript we have this handy construct: > | |
1435 GetName() || 'unknown' | |
1436 However, this conflicts with only allowing a boolean for a condition. | |
1437 Therefore the "??" operator was added: > | |
1438 GetName() ?? 'unknown' | |
1439 Here you can explicitly express your intention to use the value as-is and not | |
1440 result in a boolean. This is called the |falsy-operator|. | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1441 |
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1442 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1443 Import and Export ~ |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1444 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1445 A problem of legacy Vim script is that by default all functions and variables |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1446 are global. It is possible to make them script-local, but then they are not |
22171 | 1447 available in other scripts. This defies the concept of a package that only |
1448 exports selected items and keeps the rest local. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1449 |
21825 | 1450 In Vim9 script a mechanism very similar to the JavaScript import and export |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1451 mechanism is supported. It is a variant to the existing `:source` command |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1452 that works like one would expect: |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1453 - Instead of making everything global by default, everything is script-local, |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1454 unless exported. |
22171 | 1455 - When importing a script the symbols that are imported are explicitly listed, |
1456 avoiding name conflicts and failures if functionality is added later. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1457 - The mechanism allows for writing a big, long script with a very clear API: |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1458 the exported function(s) and class(es). |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1459 - By using relative paths loading can be much faster for an import inside of a |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1460 package, no need to search many directories. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1461 - Once an import has been used, it can be cached and loading it again can be |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1462 avoided. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1463 - The Vim-specific use of "s:" to make things script-local can be dropped. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1464 |
20856 | 1465 When sourcing a Vim9 script from a legacy script, only the items defined |
1466 globally can be used, not the exported items. Alternatives considered: | |
1467 - All the exported items become available as script-local items. This makes | |
22171 | 1468 it uncontrollable what items get defined and likely soon leads to trouble. |
20856 | 1469 - Use the exported items and make them global. Disadvantage is that it's then |
1470 not possible to avoid name clashes in the global namespace. | |
1471 - Completely disallow sourcing a Vim9 script, require using `:import`. That | |
1472 makes it difficult to use scripts for testing, or sourcing them from the | |
1473 command line to try them out. | |
22171 | 1474 Note that you can also use `:import` in legacy Vim script, see above. |
20856 | 1475 |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1476 |
22328 | 1477 Compiling functions early ~ |
1478 | |
1479 Functions are compiled when called or when `:defcompile` is used. Why not | |
1480 compile them early, so that syntax and type errors are reported early? | |
1481 | |
1482 The functions can't be compiled right away when encountered, because there may | |
1483 be forward references to functions defined later. Consider defining functions | |
1484 A, B and C, where A calls B, B calls C, and C calls A again. It's impossible | |
1485 to reorder the functions to avoid forward references. | |
1486 | |
1487 An alternative would be to first scan through the file to locate items and | |
1488 figure out their type, so that forward references are found, and only then | |
1489 execute the script and compile the functions. This means the script has to be | |
1490 parsed twice, which is slower, and some conditions at the script level, such | |
1491 as checking if a feature is supported, are hard to use. An attempt was made | |
1492 to see if it works, but it turned out to be impossible to make work nicely. | |
1493 | |
1494 It would be possible to compile all the functions at the end of the script. | |
1495 The drawback is that if a function never gets called, the overhead of | |
1496 compiling it counts anyway. Since startup speed is very important, in most | |
1497 cases it's better to do it later and accept that syntax and type errors are | |
1498 only reported then. In case these errors should be found early, e.g. when | |
1499 testing, the `:defcompile` command will help out. | |
1500 | |
1501 | |
22391
a9fb7efa31d6
patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents:
22328
diff
changeset
|
1502 Why not use an embedded language? ~ |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1503 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1504 Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But |
22328 | 1505 these interfaces have never become widely used, for various reasons. When |
1506 Vim9 was designed a decision was made to make these interfaces lower priority | |
1507 and concentrate on Vim script. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1508 |
22328 | 1509 Still, plugin writers may find other languages more familiar, want to use |
1510 existing libraries or see a performance benefit. We encourage plugin authors | |
1511 to write code in any language and run it as an external tool, using jobs and | |
1512 channels. We can try to make this easier somehow. | |
1513 | |
1514 Using an external tool also has disadvantages. An alternative is to convert | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1515 the tool into Vim script. For that to be possible without too much |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1516 translation, and keeping the code fast at the same time, the constructs of the |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1517 tool need to be supported. Since most languages support classes the lack of |
22171 | 1518 support for classes in Vim is then a problem. |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1519 |
22328 | 1520 |
1521 Classes ~ | |
1522 | |
1523 Vim supports a kind-of object oriented programming by adding methods to a | |
1524 dictionary. With some care this can be made to work, but it does not look | |
1525 like real classes. On top of that, it's quite slow, because of the use of | |
1526 dictionaries. | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1527 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1528 The support of classes in Vim9 script is a "minimal common functionality" of |
22171 | 1529 class support in most languages. It works much like Java, which is the most |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1530 popular programming language. |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1531 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1532 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1533 |
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1534 vim:tw=78:ts=8:noet:ft=help:norl: |