Vyper: Custom constants of lists

Created on 22 Jan 2019  路  6Comments  路  Source: vyperlang/vyper

Version Information

  • vyper Version: 0.1.0 beta 7

What's your issue about?

I'm not sure whether this is designed or a bug but Vyper doesn't support custom constants of lists.
It'd be better to support it if there is no concern?

# Invalid value for constant type, expected bytes[8] got bytes[8] instead
BYTE_LIST: constant(bytes[8]) = 'abcdefgh'

# 'ListType' object has no attribute 'is_literal'
BYTE32_LIST: constant(bytes32[2]) = [0x0000000000000000000000000000000000000000000000000000000000000000,
                                    0x0000000000000000000000000000000000000000000000000000000000000000]

# 'ListType' object has no attribute 'is_literal'
ZERO_LIST: constant(int128[8]) = [0, 0, 0, 0, 0, 0, 0, 0]

# 'ListType' object has no attribute 'is_literal'
ZERO_LIST: constant(uint256[8]) = [convert(0, uint256), convert(0, uint256), convert(0, uint256), convert(0, uint256), convert(0, uint256), convert(0, uint256), convert(0, uint256), convert(0, uint256)]

Cute Animal Picture

image

Approved Discussion

All 6 comments

Hmmm I don't think we ever considered lists to be constants, not that could not be - just that it never was considered.
So now it begs the question - should we or not ?

Looks like the use case here is for an "empty list" or a list full of empty entries. @nrryuya are you looking for a way to assert that a list has not been set yet?

Well it's not the only use case, I can easily imagine something like a making sure you have set of values like:

VALID_STATES:  constant(int128) = [1, 33, 44, 55 .. ]

Or another form of storing owners?

OWNERS: constant(address[3]) = [....]

Just thinking out loud - on what it could be used for.

Case 1 would actually be better served by enums (which we don't have... yet). Basically something like:

enum(int128) State:
    A = 1
    B = 33
    C = 44
    D = 55

Case 2 is typically done dynamically through a constructor. Probably not the best idea to hardcode a collection of addresses, since they might change on every deployment and you would have to update the code, but I can see a _limited_ rationale of how that could be useful for something like templated programs (but basically, just use constructor args)

Looks like the use case here is for an "empty list" or a list full of empty entries. @nrryuya are you looking for a way to assert that a list has not been set yet?

Yep. I found this issue when I wanted to return a list full of 0 in my BigInt.vy which represents a zero in a big number.

I'm also not confident about the use case of this feature...
As an example, we can use constants of a list of primes in programs like RSA Accumulator.

PRIMES: constant(int128[100]) = [2, 3, 5, ...]

List of primes might be a good one!

Was this page helpful?
0 / 5 - 0 ratings