Mercurial > vim
comparison runtime/doc/vim9.txt @ 24888:b6ac4ed5e2d2 v8.2.2982
patch 8.2.2982: Vim9: future commands are not reserved yet
Commit: https://github.com/vim/vim/commit/742357718000927d652b1a98d313a3950571c8ec
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Jun 12 14:53:05 2021 +0200
patch 8.2.2982: Vim9: future commands are not reserved yet
Problem: Vim9: future commands are not reserved yet.
Solution: Add commands to be implemented later. Make "this" a reserved
name.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 12 Jun 2021 15:00:03 +0200 |
parents | e69e7133c9cf |
children | fd37be6dc258 |
comparison
equal
deleted
inserted
replaced
24887:670e100e6be6 | 24888:b6ac4ed5e2d2 |
---|---|
1 *vim9.txt* For Vim version 8.2. Last change: 2021 May 26 | 1 *vim9.txt* For Vim version 8.2. Last change: 2021 Jun 12 |
2 | 2 |
3 | 3 |
4 VIM REFERENCE MANUAL by Bram Moolenaar | 4 VIM REFERENCE MANUAL by Bram Moolenaar |
5 | 5 |
6 | 6 |
167 `:def` has no options like `:function` does: "range", "abort", "dict" or | 167 `:def` has no options like `:function` does: "range", "abort", "dict" or |
168 "closure". A `:def` function always aborts on an error (unless `:silent!` was | 168 "closure". A `:def` function always aborts on an error (unless `:silent!` was |
169 used for the command or inside a `:try` block), does not get a range passed | 169 used for the command or inside a `:try` block), does not get a range passed |
170 cannot be a "dict" function, and can always be a closure. | 170 cannot be a "dict" function, and can always be a closure. |
171 | 171 |
172 Later classes will be added, which replaces the "dict function" mechanism. | |
173 For now you will need to pass the dictionary explicitly: > | |
174 def DictFunc(d: dict<any>, arg: string) | |
175 echo d[arg] | |
176 enddef | |
177 var d = {item: 'value', func: DictFunc} | |
178 d.func(d, 'item') | |
179 | |
172 The argument types and return type need to be specified. The "any" type can | 180 The argument types and return type need to be specified. The "any" type can |
173 be used, type checking will then be done at runtime, like with legacy | 181 be used, type checking will then be done at runtime, like with legacy |
174 functions. | 182 functions. |
175 | 183 |
176 Arguments are accessed by name, without "a:", just like any other language. | 184 Arguments are accessed by name, without "a:", just like any other language. |
443 and for a lambda. Also, when a "{" is found the parser needs to figure out if | 451 and for a lambda. Also, when a "{" is found the parser needs to figure out if |
444 it is the start of a lambda or a dictionary, which is now more complicated | 452 it is the start of a lambda or a dictionary, which is now more complicated |
445 because of the use of argument types. | 453 because of the use of argument types. |
446 | 454 |
447 To avoid these problems Vim9 script uses a different syntax for a lambda, | 455 To avoid these problems Vim9 script uses a different syntax for a lambda, |
448 which is similar to Javascript: > | 456 which is similar to JavaScript: > |
449 var Lambda = (arg) => expression | 457 var Lambda = (arg) => expression |
450 | 458 |
451 No line break is allowed in the arguments of a lambda up to and including the | 459 No line break is allowed in the arguments of a lambda up to and including the |
452 "=>". This is OK: > | 460 "=>". This is OK: > |
453 filter(list, (k, v) => | 461 filter(list, (k, v) => |
520 And when a dict spans multiple lines: > | 528 And when a dict spans multiple lines: > |
521 var mydict = { | 529 var mydict = { |
522 one: 1, | 530 one: 1, |
523 two: 2, | 531 two: 2, |
524 } | 532 } |
525 Function call: > | 533 With a function call: > |
526 var result = Func( | 534 var result = Func( |
527 arg1, | 535 arg1, |
528 arg2 | 536 arg2 |
529 ) | 537 ) |
530 | 538 |
553 at the start of the line indicates line continuation: > | 561 at the start of the line indicates line continuation: > |
554 autocmd BufNewFile *.match if condition | 562 autocmd BufNewFile *.match if condition |
555 | echo 'match' | 563 | echo 'match' |
556 | endif | 564 | endif |
557 | 565 |
566 Note that this means that in heredoc the first line cannot be a bar: > | |
567 var lines =<< trim END | |
568 | this doesn't work | |
569 END | |
570 Either use an empty line at the start or do not use heredoc. Or temporarily | |
571 add the "C" flag to 'cpoptions': > | |
572 set cpo+=C | |
573 var lines =<< trim END | |
574 | this doesn't work | |
575 END | |
576 set cpo-=C | |
577 If the heredoc is inside a function 'cpoptions' must be set before :def and | |
578 restored after the :enddef. | |
579 | |
580 In places where line continuation with a backslash is still needed, such as | |
581 splitting up a long Ex command, comments can start with #\ instead of "\: > | |
582 syn region Text | |
583 \ start='foo' | |
584 #\ comment | |
585 \ end='bar' | |
586 | |
558 < *E1050* | 587 < *E1050* |
559 To make it possible for the operator at the start of the line to be | 588 To make it possible for the operator at the start of the line to be |
560 recognized, it is required to put a colon before a range. This will add | 589 recognized, it is required to put a colon before a range. This example will |
561 "start" and print: > | 590 add "start" and print: > |
562 var result = start | 591 var result = start |
563 + print | 592 + print |
564 Like this: > | 593 Like this: > |
565 var result = start + print | 594 var result = start + print |
566 | 595 |
608 2] [3, | 637 2] [3, |
609 4] | 638 4] |
610 < This does not work: > | 639 < This does not work: > |
611 echo [1, 2] | 640 echo [1, 2] |
612 [3, 4] | 641 [3, 4] |
642 - In some cases it is difficult for Vim to parse a command, especially when | |
643 commands are used as an argument to another command, such as `windo`. In | |
644 those cases the line continuation with a backslash has to be used. | |
613 | 645 |
614 | 646 |
615 White space ~ | 647 White space ~ |
616 | 648 |
617 Vim9 script enforces proper use of white space. This is no longer allowed: > | 649 Vim9 script enforces proper use of white space. This is no longer allowed: > |
1238 export var someValue = ... | 1270 export var someValue = ... |
1239 export final someValue = ... | 1271 export final someValue = ... |
1240 export const someValue = ... | 1272 export const someValue = ... |
1241 export def MyFunc() ... | 1273 export def MyFunc() ... |
1242 export class MyClass ... | 1274 export class MyClass ... |
1275 export interface MyClass ... | |
1243 | 1276 |
1244 As this suggests, only constants, variables, `:def` functions and classes can | 1277 As this suggests, only constants, variables, `:def` functions and classes can |
1245 be exported. {not implemented yet: export class} | 1278 be exported. {not implemented yet: class, interface} |
1246 | 1279 |
1247 *E1042* | 1280 *E1042* |
1248 `:export` can only be used in Vim9 script, at the script level. | 1281 `:export` can only be used in Vim9 script, at the script level. |
1249 | 1282 |
1250 | 1283 |
1340 Most of Vim9 script can be created without this functionality, and since | 1373 Most of Vim9 script can be created without this functionality, and since |
1341 implementing classes is going to be a lot of work, it is left for the future. | 1374 implementing classes is going to be a lot of work, it is left for the future. |
1342 For now we'll just make sure classes can be added later. | 1375 For now we'll just make sure classes can be added later. |
1343 | 1376 |
1344 Thoughts: | 1377 Thoughts: |
1345 - `class` / `endclass`, everything in one file | 1378 - `class` / `endclass`, the whole class must be in one file |
1346 - Class names are always CamelCase | 1379 - Class names are always CamelCase (to avoid a name clash with builtin types) |
1347 - Single constructor | 1380 - A single constructor called "constructor" |
1348 - Single inheritance with `class ThisClass extends BaseClass` | 1381 - Single inheritance with `class ThisClass extends BaseClass` |
1349 - `abstract class` | 1382 - `abstract class` (class with incomplete implementation) |
1350 - `interface` (Abstract class without any implementation) | 1383 - `interface` / `endinterface` (abstract class without any implementation) |
1351 - `class SomeClass implements SomeInterface` | 1384 - `class SomeClass implements SomeInterface` |
1352 - Generics for class: `class <Tkey, Tentry>` | 1385 - Generics for class: `class <Tkey, Tentry>` |
1353 - Generics for function: `def <Tkey> GetLast(key: Tkey)` | 1386 - Generics for function: `def <Tkey> GetLast(key: Tkey)` |
1354 | 1387 |
1355 Again, much of this is from TypeScript. | 1388 Again, much of this is from TypeScript with a slightly different syntax. |
1356 | 1389 |
1357 Some things that look like good additions: | 1390 Some things that look like good additions: |
1358 - Use a class as an interface (like Dart) | 1391 - Use a class as an interface (like Dart) |
1359 - Extend a class with methods, using an import (like Dart) | 1392 - Extend a class with methods, using an import (like Dart) |
1393 - Mixins | |
1394 - For testing: Mock mechanism | |
1360 | 1395 |
1361 An important class that will be provided is "Promise". Since Vim is single | 1396 An important class that will be provided is "Promise". Since Vim is single |
1362 threaded, connecting asynchronous operations is a natural way of allowing | 1397 threaded, connecting asynchronous operations is a natural way of allowing |
1363 plugins to do their work without blocking the user. It's a uniform way to | 1398 plugins to do their work without blocking the user. It's a uniform way to |
1364 invoke callbacks and handle timeouts and errors. | 1399 invoke callbacks and handle timeouts and errors. |
1400 | |
1401 Some examples: > | |
1402 | |
1403 abstract class Person | |
1404 static const prefix = 'xxx' | |
1405 var name: string | |
1406 | |
1407 def constructor(name: string) | |
1408 this.name = name; | |
1409 enddef | |
1410 | |
1411 def display(): void | |
1412 echo name | |
1413 enddef | |
1414 | |
1415 abstract def find(string): Person | |
1416 endclass | |
1365 | 1417 |
1366 ============================================================================== | 1418 ============================================================================== |
1367 | 1419 |
1368 9. Rationale *vim9-rationale* | 1420 9. Rationale *vim9-rationale* |
1369 | 1421 |