1- *vim9.txt* For Vim version 8.2. Last change: 2020 Sep 07
1+ *vim9.txt* For Vim version 8.2. Last change: 2020 Sep 13
22
33
44 VIM REFERENCE MANUAL by Bram Moolenaar
@@ -192,6 +192,9 @@ To intentionally avoid a variable being available later, a block can be used:
192192 }
193193 echo temp # Error!
194194
195+ Declaring a variable with a type but without an initializer will initialize to
196+ zero, false or empty.
197+
195198An existing variable cannot be assigned to with `:let ` , since that implies a
196199declaration. Global, window, tab, buffer and Vim variables can only be used
197200without `:let ` , because they are not really declared, they can also be deleted
@@ -210,6 +213,40 @@ at the script level. >
210213
211214 Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be
212215used to repeat a `:substitute ` command.
216+ *vim9-const*
217+ In legacy Vim script "const list = []" would make the variable "list"
218+ immutable and also the value. Thus you cannot add items to the list. This
219+ differs from what many languages do. Vim9 script does it like TypeScript: only
220+ "list" is immutable, the value can be changed.
221+
222+ One can use `:const ! ` to make both the variable and the value immutable. Use
223+ this for composite structures that you want to make sure will not be modified.
224+
225+ How this works: >
226+ vim9script
227+ const list = [1, 2]
228+ list = [3, 4] # Error!
229+ list[0] = 2 # OK
230+
231+ const! LIST = [1, 2]
232+ LIST = [3, 4] # Error!
233+ LIST[0] = 2 # Error!
234+ It is common to write constants as ALL_CAPS, but you don't have to.
235+
236+ The constant only applies to the value itself, not what it refers to. >
237+ cont females = ["Mary"]
238+ const! NAMES = [["John", "Peter"], females]
239+ NAMES[0] = ["Jack"] # Error!
240+ NAMES[0][0] = ["Jack"] # Error!
241+ NAMES[1] = ["Emma"] # Error!
242+ Names[1][0] = "Emma" # OK, now females[0] == "Emma"
243+
244+ Rationale: TypeScript has no way to make the value immutable. One can use
245+ immutable types, but that quickly gets complicated for nested values. And
246+ with a type cast the value can be made mutable again, which means there is no
247+ guarantee the value won't change. Vim supports immutable values, in legacy
248+ script this was done with `:lockvar ` . But that is an extra statement and also
249+ applies to nested values. Therefore the solution to use `:const ! ` .
213250
214251 *E1092*
215252Declaring more than one variable at a time, using the unpack notation, is
@@ -408,7 +445,7 @@ for using a list or job. This is very much like JavaScript, but there are a
408445few exceptions.
409446
410447 type TRUE when ~
411- bool v:true
448+ bool v:true or 1
412449 number non-zero
413450 float non-zero
414451 string non-empty
@@ -946,26 +983,41 @@ declarations: >
946983 Expression evaluation was already close to what JavaScript and other languages
947984are doing. Some details are unexpected and can be fixed. For example how the
948985|| and && operators work. Legacy Vim script: >
949- let result = 44
986+ let value = 44
950987 ...
951- return result || 0 # returns 1
988+ let result = value || 0 # result == 1
952989
953990 Vim9 script works like JavaScript/TypeScript, keep the value: >
954- let result = 44
991+ let value = 44
955992 ...
956- return result || 0 # returns 44
957-
958- On the other hand, overloading "+" to use both for addition and string
959- concatenation goes against legacy Vim script and often leads to mistakes.
960- For that reason we will keep using ".." for string concatenation. Lua also
961- uses ".." this way.
993+ let result = value || 0 # result == 44
962994
963995 There is no intention to completely match TypeScript syntax and semantics. We
964996just want to take those parts that we can use for Vim and we expect Vim users
965- are happy with. TypeScript is a complex language with its own advantages and
966- disadvantages. People used to other languages (Java, Python, etc.) will also
967- find things in TypeScript that they do not like or do not understand. We'll
968- try to avoid those things.
997+ will be happy with. TypeScript is a complex language with its own advantages
998+ and disadvantages. To get an idea of the disadvantages read the book:
999+ "JavaScript: The Good Parts". Or find the article "TypeScript: the good
1000+ parts" and read the "Things to avoid" section.
1001+
1002+ People used to other languages (Java, Python, etc.) will also find things in
1003+ TypeScript that they do not like or do not understand. We'll try to avoid
1004+ those things.
1005+
1006+ Specific items from TypeScript we avoid:
1007+ - Overloading "+", using it both for addition and string concatenation. This
1008+ goes against legacy Vim script and often leads to mistakes. For that reason
1009+ we will keep using ".." for string concatenation. Lua also uses ".." this
1010+ way. And it allows for conversion to string for more values.
1011+ - TypeScript can use an expression like "99 || 'yes' " in a condition, but
1012+ cannot assign the value to a boolean. That is inconsistent and can be
1013+ annoying. Vim recognizes an expression with && or || and allows using the
1014+ result as a bool.
1015+ - TypeScript considers an empty string as Falsy, but an empty list or dict as
1016+ Truthy. That is inconsistent. In Vim an empty list and dict are also
1017+ Falsy.
1018+ - TypeScript has various "Readonly" types, which have limited usefulness,
1019+ since a type cast can remove the immutable nature. Vim locks the value,
1020+ which is more flexible, but is only checked at runtime.
9691021
9701022
9711023Import and Export ~
0 commit comments