comparison runtime/doc/vim9.txt @ 22272:eb1f5f618c75 v8.2.1685

patch 8.2.1685: Vim9: cannot declare a constant value Commit: https://github.com/vim/vim/commit/0b4c66c67a083f25816b9cdb8e76a41e02d9f560 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Sep 14 21:39:44 2020 +0200 patch 8.2.1685: Vim9: cannot declare a constant value Problem: Vim9: cannot declare a constant value. Solution: Introduce ":const!".
author Bram Moolenaar <Bram@vim.org>
date Mon, 14 Sep 2020 21:45:04 +0200
parents d4c7b3e9cd17
children 75ff30a78189
comparison
equal deleted inserted replaced
22271:0440d470ae70 22272:eb1f5f618c75
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
2 2
3 3
4 VIM REFERENCE MANUAL by Bram Moolenaar 4 VIM REFERENCE MANUAL by Bram Moolenaar
5 5
6 6
190 let temp = 'temp' 190 let temp = 'temp'
191 ... 191 ...
192 } 192 }
193 echo temp # Error! 193 echo temp # Error!
194 194
195 Declaring a variable with a type but without an initializer will initialize to
196 zero, false or empty.
197
195 An existing variable cannot be assigned to with `:let`, since that implies a 198 An existing variable cannot be assigned to with `:let`, since that implies a
196 declaration. Global, window, tab, buffer and Vim variables can only be used 199 declaration. Global, window, tab, buffer and Vim variables can only be used
197 without `:let`, because they are not really declared, they can also be deleted 200 without `:let`, because they are not really declared, they can also be deleted
198 with `:unlet`. 201 with `:unlet`.
199 202
208 g:global = 'value' 211 g:global = 'value'
209 let Funcref = g:ThatFunction 212 let Funcref = g:ThatFunction
210 213
211 Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be 214 Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be
212 used to repeat a `:substitute` command. 215 used 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!`.
213 250
214 *E1092* 251 *E1092*
215 Declaring more than one variable at a time, using the unpack notation, is 252 Declaring more than one variable at a time, using the unpack notation, is
216 currently not supported: > 253 currently not supported: >
217 let [v1, v2] = GetValues() # Error! 254 let [v1, v2] = GetValues() # Error!
406 Any type of variable can be used as a condition, there is no error, not even 443 Any type of variable can be used as a condition, there is no error, not even
407 for using a list or job. This is very much like JavaScript, but there are a 444 for using a list or job. This is very much like JavaScript, but there are a
408 few exceptions. 445 few exceptions.
409 446
410 type TRUE when ~ 447 type TRUE when ~
411 bool v:true 448 bool v:true or 1
412 number non-zero 449 number non-zero
413 float non-zero 450 float non-zero
414 string non-empty 451 string non-empty
415 blob non-empty 452 blob non-empty
416 list non-empty (different from JavaScript) 453 list non-empty (different from JavaScript)
944 name = 'John' 981 name = 'John'
945 982
946 Expression evaluation was already close to what JavaScript and other languages 983 Expression evaluation was already close to what JavaScript and other languages
947 are doing. Some details are unexpected and can be fixed. For example how the 984 are doing. Some details are unexpected and can be fixed. For example how the
948 || and && operators work. Legacy Vim script: > 985 || and && operators work. Legacy Vim script: >
949 let result = 44 986 let value = 44
950 ... 987 ...
951 return result || 0 # returns 1 988 let result = value || 0 # result == 1
952 989
953 Vim9 script works like JavaScript/TypeScript, keep the value: > 990 Vim9 script works like JavaScript/TypeScript, keep the value: >
954 let result = 44 991 let value = 44
955 ... 992 ...
956 return result || 0 # returns 44 993 let result = value || 0 # result == 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.
962 994
963 There is no intention to completely match TypeScript syntax and semantics. We 995 There is no intention to completely match TypeScript syntax and semantics. We
964 just want to take those parts that we can use for Vim and we expect Vim users 996 just 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 997 will be happy with. TypeScript is a complex language with its own advantages
966 disadvantages. People used to other languages (Java, Python, etc.) will also 998 and disadvantages. To get an idea of the disadvantages read the book:
967 find things in TypeScript that they do not like or do not understand. We'll 999 "JavaScript: The Good Parts". Or find the article "TypeScript: the good
968 try to avoid those things. 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.
969 1021
970 1022
971 Import and Export ~ 1023 Import and Export ~
972 1024
973 A problem of legacy Vim script is that by default all functions and variables 1025 A problem of legacy Vim script is that by default all functions and variables