Skip to content
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

Closed
lacygoill opened this issue Nov 21, 2021 · 5 comments
Closed

Vim9: cannot use key from dictionary as iteration variable #9179

lacygoill opened this issue Nov 21, 2021 · 5 comments

Comments

@lacygoill
Copy link

Steps to reproduce

Run this shell command:

vim -Nu NONE -S <(cat <<'EOF'
    vim9script
    def Func()
        var d = {k: 'aaa'}
        for d.k in ['bbb', 'ccc', 'ddd']
            echo d.k
        endfor
    enddef
    Func()
EOF
)

These lines are echo'ed:

aaa
aaa
aaa

Expected behavior

Not sure. Either these lines are echo'ed:

bbb
ccc
ddd

Or an error is given:

E1234: cannot use key from dictionary as iteration variable

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:

vim9script
function Func()
    let d = {'k': 'aaa'}
    for d.k in ['bbb', 'ccc', 'ddd']
        echo d.k
    endfor
endfunction
Func()
bbb
ccc
ddd

Are we meant to be able to use a dictionary key as iteration variable in Vim9? If not, maybe it should be documented.

@bfrg
Copy link
Contributor

bfrg commented Nov 21, 2021

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:

aaa
bbb
ccc
ddd
ddd

@lacygoill
Copy link
Author

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()
aaa
aaa
aaa

No error is given, and l[0] silently fails to iterate over the members of ['bbb', 'ccc', 'ddd'].

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()
bbb
ccc
ddd

@lacygoill
Copy link
Author

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.

@brammool
Copy link
Contributor

It works in legacy script, it should also work in Vim9 script

@brammool
Copy link
Contributor

brammool commented Nov 22, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants