comparison runtime/doc/eval.txt @ 22298:07e48ee8c3bb v8.2.1698

patch 8.2.1698: cannot lock a variable in legacy Vim script like in Vim9 Commit: https://github.com/vim/vim/commit/a187c43cfe8863d48b2159d695fedcb71f8525c1 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Sep 16 21:08:28 2020 +0200 patch 8.2.1698: cannot lock a variable in legacy Vim script like in Vim9 Problem: Cannot lock a variable in legacy Vim script like in Vim9. Solution: Make ":lockvar 0" work.
author Bram Moolenaar <Bram@vim.org>
date Wed, 16 Sep 2020 21:15:05 +0200
parents f22acf6472da
children 75ff30a78189
comparison
equal deleted inserted replaced
22297:3d2dc70119a7 22298:07e48ee8c3bb
12362 the variable with |:lockvar| just after |:let|, thus: > 12362 the variable with |:lockvar| just after |:let|, thus: >
12363 :const x = 1 12363 :const x = 1
12364 < is equivalent to: > 12364 < is equivalent to: >
12365 :let x = 1 12365 :let x = 1
12366 :lockvar! x 12366 :lockvar! x
12367 < This is useful if you want to make sure the variable 12367 < NOTE: in Vim9 script `:const` works differently, see
12368 |vim9-const|
12369 This is useful if you want to make sure the variable
12368 is not modified. If the value is a List or Dictionary 12370 is not modified. If the value is a List or Dictionary
12369 literal then the items also cannot be changed: > 12371 literal then the items also cannot be changed: >
12370 const ll = [1, 2, 3] 12372 const ll = [1, 2, 3]
12371 let ll[1] = 5 " Error! 12373 let ll[1] = 5 " Error!
12372 < Nested references are not locked: > 12374 < Nested references are not locked: >
12402 get an error message: "E940: Cannot lock or unlock 12404 get an error message: "E940: Cannot lock or unlock
12403 variable {name}". 12405 variable {name}".
12404 12406
12405 [depth] is relevant when locking a |List| or 12407 [depth] is relevant when locking a |List| or
12406 |Dictionary|. It specifies how deep the locking goes: 12408 |Dictionary|. It specifies how deep the locking goes:
12409 0 Lock the variable {name} but not its
12410 value.
12407 1 Lock the |List| or |Dictionary| itself, 12411 1 Lock the |List| or |Dictionary| itself,
12408 cannot add or remove items, but can 12412 cannot add or remove items, but can
12409 still change their values. 12413 still change their values.
12410 2 Also lock the values, cannot change 12414 2 Also lock the values, cannot change
12411 the items. If an item is a |List| or 12415 the items. If an item is a |List| or
12415 3 Like 2 but for the |List| / 12419 3 Like 2 but for the |List| /
12416 |Dictionary| in the |List| / 12420 |Dictionary| in the |List| /
12417 |Dictionary|, one level deeper. 12421 |Dictionary|, one level deeper.
12418 The default [depth] is 2, thus when {name} is a |List| 12422 The default [depth] is 2, thus when {name} is a |List|
12419 or |Dictionary| the values cannot be changed. 12423 or |Dictionary| the values cannot be changed.
12420 *E743* 12424
12425 Example with [depth] 0: >
12426 let mylist = [1, 2, 3]
12427 lockvar 0 mylist
12428 let mylist[0] = 77 " OK
12429 call add(mylist, 4] " OK
12430 let mylist = [7, 8, 9] " Error!
12431 < *E743*
12421 For unlimited depth use [!] and omit [depth]. 12432 For unlimited depth use [!] and omit [depth].
12422 However, there is a maximum depth of 100 to catch 12433 However, there is a maximum depth of 100 to catch
12423 loops. 12434 loops.
12424 12435
12425 Note that when two variables refer to the same |List| 12436 Note that when two variables refer to the same |List|