Numbers as dictionary keys

17 views
Skip to first unread message

Ernie Rael

unread,
Apr 28, 2022, 9:41:17 AM4/28/22
to vim...@googlegroups.com
In vim9.txt there's

The key type can be string, number, bool or float.  Other types result in an
error.  A number can be given with and without the []:

Don't know if the following shows a bug in implementation or documentation.
The two dictionaries are not equal.

    vim9script

    var d1 = {[000123]: 'foo'}
    echo d1
    ### output {'123': 'foo'}

    var d2 = {000123: 'foo'}
    echo d2
    ### output {'000123': 'foo'}

    if d1 == d2
        echo 'EQUAL'
    else
        echo 'NOT EQUAL'
    endif

Bram Moolenaar

unread,
Apr 28, 2022, 10:27:27 AM4/28/22
to vim...@googlegroups.com, Ernie Rael
This is correct. The key eventually is always a string. If you use a
number and convert it to a string then leading zeros are dropped. If
you use a string with leading zeros, they are kept. This is in the help
below ":he E717".

--
hundred-and-one symptoms of being an internet addict:
86. E-mail Deficiency Depression (EDD) forces you to e-mail yourself.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Ernie Rael

unread,
Apr 28, 2022, 11:30:12 AM4/28/22
to Bram Moolenaar, vim...@googlegroups.com
On 4/28/22 7:27 AM, Bram Moolenaar wrote:
> Ernie Rael wrote:
>
>> In vim9.txt there's
>>
>> The key type can be string, number, bool or float.  Other types result in an
>> error.  A number can be given with and without the []:
>>
>> Don't know if the following shows a bug in implementation or documentation.
>> The two dictionaries are not equal.
>>
>>     vim9script
>>
>>     var d1 = {[000123]: 'foo'}
>>     echo d1
>>     ### output {'123': 'foo'}
>>
>>     var d2 = {000123: 'foo'}
>>     echo d2
>>     ### output {'000123': 'foo'}
>>
>>     if d1 == d2
>>         echo 'EQUAL'
>>     else
>>         echo 'NOT EQUAL'
>>     endif
> This is correct. The key eventually is always a string. If you use a
> number and convert it to a string then leading zeros are dropped. If
> you use a string with leading zeros, they are kept. This is in the help
> below ":he E717".
>
E717 says

You can use a Number, it will be converted
...Note that the String '04' and the Number 04 are different,
since the Number will be converted to the String '4'.

According to that, in the example I started with, {000123: 'foo'}, the
number should be converted. But it is not being converted. Try

vim9script
echo {000123: 'foo'}

-ernie

Bram Moolenaar

unread,
Apr 28, 2022, 11:52:54 AM4/28/22
to vim...@googlegroups.com, Ernie Rael, Bram Moolenaar

Ernie Rael wrote:

> >> In vim9.txt there's
> >>
> >> The key type can be string, number, bool or float.  Other types res> ult in an
That is not a number but a string. As you noticed before, using [expr]
does use 000123 as a number, then converts it to a string.
Using the {key: value} syntax the "key" part is always a string.

--
hundred-and-one symptoms of being an internet addict:
88. Every single time you press the 'Get mail' button...it does get new mail.

Ernie Rael

unread,
Apr 28, 2022, 12:48:49 PM4/28/22
to Bram Moolenaar, vim...@googlegroups.com
Isn't this doc in vim9.txt wrong (or at least highly misleading)

The key type can be string, number, bool or float.  Other types
result in an
error.  A number can be given with and without the []: >
    var dict = {123: 'without', [456]: 'with'}
    echo dict
    {'456': 'with', '123': 'without'}

It is not the same "with and without"

At E717, where it is talking about "Dictionary creation", it says
"Number will be converted to String". But that might be a legacy script
thing and so out of my experience.

-ernie

Bram Moolenaar

unread,
Apr 28, 2022, 2:09:38 PM4/28/22
to vim...@googlegroups.com, Ernie Rael, Bram Moolenaar

Ernie Rael wrote:

> >>>> In vim9.txt there's
> >>>>
> >>>> The key type can be string, number, bool or float.=C2=A0 Other types r=
> es> ult in an
> >>>> error.=C2=A0 A number can be given with and without the []:
> >>>>
> >>>> Don't know if the following shows a bug in implementation or documenta=
> ti> on.
> >>>> The two dictionaries are not equal.
> >>>>
> >>>> =C2=A0=C2=A0=C2=A0 vim9script
> >>>>
> >>>> =C2=A0=C2=A0=C2=A0 var d1 =3D {[000123]: 'foo'}
> >>>> =C2=A0=C2=A0=C2=A0 echo d1
> >>>> =C2=A0=C2=A0=C2=A0 ### output {'123': 'foo'}
> >>>>
> >>>> =C2=A0=C2=A0=C2=A0 var d2 =3D {000123: 'foo'}
> >>>> =C2=A0=C2=A0=C2=A0 echo d2
> >>>> =C2=A0=C2=A0=C2=A0 ### output {'000123': 'foo'}
> >>>>
> >>>> =C2=A0=C2=A0=C2=A0 if d1 =3D=3D d2
> >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 echo 'EQUAL'
> >>>> =C2=A0=C2=A0=C2=A0 else
> >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 echo 'NOT EQUAL'
> >>>> =C2=A0=C2=A0=C2=A0 endif
> >>> This is correct. The key eventually is always a string. If you use a
> >>> number and convert it to a string then leading zeros are dropped. If
> >>> you use a string with leading zeros, they are kept. This is in the hel=
This should be clearer:

The key type can be string, number, bool or float. Other types result in an
error. Without using [] the value is used as a string, keeping leading zeros.
An expression given with [] is evaluated and then converted to a string.
Leading zeros will then be dropped: >
var dict = {000123: 'without', [000456]: 'with'}
echo dict
{'456': 'with', '000123': 'without'}

--
hundred-and-one symptoms of being an internet addict:
91. It's Saturday afternoon in the middle of May and you
are on computer.
Reply all
Reply to author
Forward
0 new messages