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):
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
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.
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.
Thank you.
vim9script
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
—