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

VIP: allow variable ranges with max bounds #2527

Closed
Tracked by #2519
charles-cooper opened this issue Nov 6, 2021 · 7 comments
Closed
Tracked by #2519

VIP: allow variable ranges with max bounds #2527

charles-cooper opened this issue Nov 6, 2021 · 7 comments
Labels
VIP: Deferred VIP is not scheduled to move forward at this time
Milestone

Comments

@charles-cooper
Copy link
Member

charles-cooper commented Nov 6, 2021

Simple Summary

Add variable ranges with max bounds, e.g. for i in range(<var>, bound=100)

Motivation

Right now vyper requires for-loop ranges to be bounded at compile time. However, an extremely common use case is to iterate over some variable counter at runtime. This leads to the common idiom,

for i in range(SOME_BOUND):
    if i >= self.count:
        break
    <loop body>

This proposal allows a runtime variable counter but also requires a max bound on the range.

Specification

Add a bound keyword arg to the range builtin, and relax the restrictions on the first argument to allow it to be a non-literal variable. For instance, the above example would become

for i in range(self.count, bound=SOME_BOUND):
    <loop body>

Backwards Compatibility

None known

Dependencies

References

Copyright

Copyright and related rights waived via CC0

@charles-cooper
Copy link
Member Author

This probably becomes more relevant with dynamic arrays #1440

@fubuloubu
Copy link
Member

fubuloubu commented Nov 6, 2021

Might be more interesting to do it like a slice of the dynamic variable itself:

for item in slice(self.dynamic_array, self.start, LENGTH):
    <loop body>

self.start += LENGTH  # So next time we do it, we start ahead of last time

@charles-cooper
Copy link
Member Author

Yeah often we want access to the i value tho, so maybe we need enumerate(slice(...))

@fubuloubu
Copy link
Member

Yeah often we want access to the i value tho, so maybe we need enumerate(slice(...))

I like enumerate

@fubuloubu fubuloubu added the VIP: Deferred VIP is not scheduled to move forward at this time label Nov 22, 2021
@fubuloubu
Copy link
Member

fubuloubu commented Nov 22, 2021

Meeting notes: consider iteration over slice, and how it fits in with custom storage types #1954

@charles-cooper
Copy link
Member Author

and also how it fits in with dynamic arrays #1440

@charles-cooper
Copy link
Member Author

Implemented in #3537

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VIP: Deferred VIP is not scheduled to move forward at this time
Projects
None yet
Development

No branches or pull requests

2 participants