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.
This commit is contained in:
Bram Moolenaar
2020-11-19 18:53:18 +01:00
parent ee8b787bcd
commit 2bede173a1
9 changed files with 70 additions and 22 deletions

View File

@ -112,8 +112,7 @@ In Vi # is a command to list text with numbers. In Vim9 script you can use
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. Examples: >
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 the line with a backslash
'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.