-
-
Notifications
You must be signed in to change notification settings - Fork 806
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 usage of min with a literal to set iterators #1047
Comments
Looks like a reasonable change to me. The only problem is that if one of the parameters isn't a reasonable runtime value it could cause trouble. |
Well, I think The one modification is that since But again, since we have named constants now, you could just use that everywhere too e.g. N: constant(uint8) = 64
@private
def __main__(input: bytes[N]) -> int128:
_x: int128 = 0
_len: int128 = len(input)
for i in range(N):
_x = i
return _x We have a call tomorrow, let's discuss! |
We discussed this issue at our meeting yesterday. We came to the conclusion that we would need more details from the specific use case that is required, however whitelisting range(min()) isn't safe. but we did come with two sensible use cases: 1.) iterating per byte until len of a bytearray. input: bytes[100] = "test data"
for idx, b in enumerate(input): 2.) whitelisting a statement of input: bytes[100] = "test data"
for i in enumerate(input): @SamuelTroper What are your thoughts? Does 1 or 2 cover the use case? |
I'd like to note that |
@jacqueswww assuming 2 is supposed to be
I think that would solve the use case I was thinking of. Additionally, if that were implemented you could effectively get the result of 1 just by slicing the input to one byte each loop. Also, for clarity enumerate is not currently implemented, correct? Use case I was envisioning was storing multiple different types (with very different sizes) of data in the same byte array with some bytes used to identify what type of data was being stored. |
I think 2 is actually Your use case sounds sort of like RLP decoding. We have a macro for that. |
@fubuloubu I hadn't looked into RLP much before but man that's a nice standard. Thanks for pointing that out. |
The following works: @public
def test(input: bytes32[64]):
for b in input:
... But you cannot iterate over a bytes object itself: @public
def test(input: bytes[64]):
for b in input:
...
The solution for this would be implement the |
due to offline feedback, probably will deprecate this proposal in favor of #2527 |
Simple Summary
Allow usage of min with a literal to set iterators e.g. min(22, len(someBytes))
Abstract
Currently, iterators must be set with a literal. A minimum which contains a literal reasonably should be no larger than said literal, but could theoretically be smaller based upon the other value.
Motivation
This could reduce the number of loops used (and any calls contained within) in situations where the minimum number of loops may fall below a set value e.g. looping over a byte array of max length 64, but it may be shorter.
Specification
I would expect a contract similar to the following to compile
Backwards Compatibility
Yes, doesn't change any previously compatible literal inputs for range
Copyright
Copyright and related rights waived via CC0
The text was updated successfully, but these errors were encountered: