Mercurial > vim
diff runtime/doc/vim9.txt @ 22936:00b0275ffe7f v8.2.2015
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Commit: https://github.com/vim/vim/commit/2bede173a177e227e6224a8713f5b88a38d011af
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Nov 19 18:53:18 2020 +0100
patch 8.2.2015: Vim9: literal dict #{} is not like any other language
Problem: Vim9: literal dict #{} is not like any other language.
Solution: Support the JavaScript syntax.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 19 Nov 2020 19:00:06 +0100 |
parents | 5b7ea82bc18f |
children | e7c125224b1a |
line wrap: on
line diff
--- a/runtime/doc/vim9.txt +++ b/runtime/doc/vim9.txt @@ -112,8 +112,7 @@ In Vi # is a command to list text with n 101 number To improve readability there must be a space between a command and the # -that starts a comment. Note that #{ is the start of a dictionary, therefore -it does not start a comment. +that starts a comment. Vim9 functions ~ @@ -303,8 +302,7 @@ identifier or can't be an Ex command. E myList->add(123) g:myList->add(123) [1, 2, 3]->Process() - #{a: 1, b: 2}->Process() - {'a': 1, 'b': 2}->Process() + {a: 1, b: 2}->Process() "foobar"->Process() ("foobar")->Process() 'foobar'->Process() @@ -346,7 +344,7 @@ those cases there is no need to prefix t 'two', ] And when a dict spans multiple lines: > - var mydict = #{ + var mydict = { one: 1, two: 2, } @@ -430,6 +428,27 @@ No curly braces expansion ~ |curly-braces-names| cannot be used. +Dictionary literals ~ + +Traditionally Vim has supported dictionary literals with a {} syntax: > + let dict = {'key': value} + +Later it became clear that using a simple key name is very common, thus +literally dictionaries were introduced in a backwards compatible way: > + let dict = #{key: value} + +However, this #{} syntax is unlike any existing language. As it appears that +using a literaly key is much more common than using an expression, and +considering that JavaScript uses this syntax, using the {} form for dictionary +literals was considered a much more useful syntax. In Vim9 script the {} form +uses literal keys: > + let dict = {key: value} + +In case an expression needs to be used for the key, square brackets can be +used, just like in JavaScript: > + let dict = {["key" .. nr]: value} + + No :xit, :t, :append, :change or :insert ~ These commands are too easily confused with local variable names.