[vim/vim] Vim9: unable to add a number value to a nested dictionary (#8102)

17 views
Skip to first unread message

Andy Stewart

unread,
Apr 12, 2021, 1:08:22 PM4/12/21
to vim/vim, Subscribed

I want to add a number value to a nested dictionary but I keep getting E1012: Type mismatch; expected string but got number.

I assume it's because the nested dictionary's values' type is inferred to be string. I can't find a way to declare a type of any, nor a way to bypass the inferred-type check.

Describe the bug

Source this script:

vim9script



# final foo = [

var foo: list<dict<any>> = [

        {

          a: "Cat",

          b: false,

          c: [

            {x: "Dog"},

          ]

        }

      ]



var counter = 0



for p in foo

  for q in p.c

    counter += 1

    q.counter = counter    # ← this is the line that generates the E1012 error

  endfor

endfor

Environment (please complete the following information):

  • Vim 8.2.2566
  • OS: macOS 10.14.6
  • Terminal: iTerm2


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

lacygoill

unread,
Apr 12, 2021, 1:23:31 PM4/12/21
to vim/vim, Subscribed

Not sure but this looks like a bug I noticed a long time ago, but didn't report yet (because I wasn't sure).

This snippet raises E1012:

vim9script
var d: list<dict<any>> = [{a: 0}]
for e in d
    e = {a: 0, b: ''}
endfor
E1012: Type mismatch; expected dict<number> but got dict<any>

But the same code doesn't raise any error when compiled:

vim9script
def Func()
    var d: list<dict<any>> = [{a: 0}]
    for e in d
        e = {a: 0, b: ''}
    endfor
enddef
Func()
no error

This seems inconsistent.

There is a sentence at :h vim9 (look for the pattern For loop) which says:

Generally, you should not change the list that is iterated over. Make a copy
first if needed.

I wonder whether that's somehow relevant.

Andy Stewart

unread,
Apr 12, 2021, 1:47:46 PM4/12/21
to vim/vim, Subscribed

Make a copy first if needed.

This works:

- q.counter = counter
+ p.c = extendnew(q, {counter: counter})

But it would be nice to know if the type checking can be wrangled into allowing the nested dict to be updated in place.

Bram Moolenaar

unread,
Apr 13, 2021, 2:53:58 PM4/13/21
to vim/vim, Subscribed

Closed #8102 via f225396.

Bram Moolenaar

unread,
Apr 13, 2021, 3:29:24 PM4/13/21
to vim/vim, Subscribed


> Not sure but this looks like [a bug](https://github.com/lacygoill/wiki/blob/master/bug/vim.md#-6) I noticed a long time ago, but didn't report yet (because I wasn't sure).
>
> This snippet raises `E1012`:
> ```vim

> vim9script
> var d: list<dict<any>> = [{a: 0}]
> for e in d
> e = {a: 0, b: ''}
> endfor
> ```
> E1012: Type mismatch; expected dict<number> but got dict<any>

This works now:
```vim

vim9script
var d: list<dict<any>> = [{a: 0}]
for e in d
e.b = ''
endfor
```
Assigning to "e" is actually not allowed, it doesn't work. I'll make
another patch for that.


--
From "know your smileys":
:-| :-| Deja' vu!

/// 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 ///

Andy Stewart

unread,
Apr 14, 2021, 3:52:31 AM4/14/21
to vim/vim, Subscribed

Thank you.

Leeeooo19888

unread,
Apr 14, 2021, 6:43:32 AM4/14/21
to vim/vim, Subscribed

vim9script

final foo = [

var foo: list<dict> = [


{
a: "Cat",
b: false,
c: [
{x: "Dog"},
]
}
]

var counter = 0

for p in foo
for q in p.c
counter += 1
q.counter = counter # ← this is the line that generates the E1012 error
endfor
endfor

Reply all
Reply to author
Forward
0 new messages