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

Cannot modify loop variable issue when trying to append something to an array during a loop. #4163

Open
rockorama opened this issue Jun 22, 2024 · 0 comments · May be fixed by #4164
Open

Cannot modify loop variable issue when trying to append something to an array during a loop. #4163

rockorama opened this issue Jun 22, 2024 · 0 comments · May be fixed by #4164

Comments

@rockorama
Copy link
Contributor

rockorama commented Jun 22, 2024

Version Information

  • vyper Version (output of vyper --version): 0.4.0
  • OS: osx
  • Python Version (output of python --version): 3.10.0

What's your issue about?

When iterating through an array of addresses, I have to append to another array the ones already processed.
When trying to append to that array I get the compilation error Cannot modify loop variable

It works if I don't iterate through the addresses array, but use indices to do so instead.

@external
def foo():
    remainingToRepay: uint256 = 0
    alreadyDone: DynArray[address, 10] = []
    _assets: DynArray[address, 10] = []
    for a: address in _assets:
        if remainingToRepay == 0:
            break
        if not a in alreadyDone:
            continue
        alreadyDone.append(a)

works with:

  for i: uint256 in range(len(_asserts), bound=MAX_ASSETS):
        a: address = _assets[i]
        if remainingToRepay == 0:
            break
        if not a in alreadyDone:
            continue
        alreadyDone.append(a)
@charles-cooper charles-cooper linked a pull request Jun 22, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant