New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vim9: cannot use key from dictionary as iteration variable #9179
Comments
Some languages allow using a dictionary key for iteration. For example, in Python: #!/usr/bin/env python3
d = {"k": "aaa"}
print(d["k"])
for d["k"] in ["bbb", "ccc", "ddd"]:
print(d["k"])
print(d["k"]) Output:
|
Same behavior when we iterate with a list member: vim9script
def Func()
var l = ['aaa']
for l[0] in ['bbb', 'ccc', 'ddd']
echo l[0]
endfor
enddef
Func()
No error is given, and And again, no issue in legacy: vim9script
function Func()
let l = ['aaa']
for l[0] in ['bbb', 'ccc', 'ddd']
echo l[0]
endfor
endfunction
Func()
|
Just to be clear: if this syntax is deemed too confusing, that's fine. What is not OK is that Vim silently ignores the iterated list. |
It works in legacy script, it should also work in Vim9 script |
Let's first look at script level. Currently this gives an error:
var i = 0
for i in [1, 2, 3]
echo i
endfor
The reason is that it may accidentally use variable "i". It's like
declaring a variable that was already declared.
Now this also means that using a list or dict member gives the error for
the variable already existing:
var l = [0]
for l[0] in [1, 2, 3]
echo l[0]
endfor
If we would allow this, because it's a member and not a declared
variable, things get confusing. So perhaps re-using a variable should
not be an error? That might lead to mistakes.
To be consistent, we should just say that the loop variable must not
exist yet. That disallows assigning to a list or dict member, but is
that really a problem? In case you need it there is just one extra
line:
var l = [0]
for i in [1, 2, 3]
l[0] = i
echo l[0]
endfor
It's a small price to pay for simplicity.
Extending this to compiled functions, it should work the same way.
Then the problem is that this does not give an error:
def Func()
var l = [0]
for l[0] in [1, 2, 3]
echomsg l[0]
endfor
enddef
I'll add the missing error check. Then it should work consistently.
…--
BRIDGEKEEPER: What is your favorite colour?
GAWAIN: Blue ... No yelloooooww!
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
/// Bram Moolenaar -- ***@***.*** -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
|
Steps to reproduce
Run this shell command:
These lines are echo'ed:
Expected behavior
Not sure. Either these lines are echo'ed:
Or an error is given:
In any case, I think the current situation is not OK, because it leads to unexpected results, which might be hard to debug.
Operating system
Ubuntu 20.04.3 LTS
Version of Vim
8.2 Included patches: 1-3640
Additional Context
No issue in legacy:
Are we meant to be able to use a dictionary key as iteration variable in Vim9? If not, maybe it should be documented.
The text was updated successfully, but these errors were encountered: