1

Is it possible to have a mapping inside a struct for vyper? For example:

struct Teddy:
    id: uint256
    year: uint256
    component: map(uint256, uint256)

teddies: private(map(uint256, Teddy))

So for the above, component takes a component number as a key and returns the id of the component (e.g. component[0] = 1234, component[1] = 2438...). If this is possible, how do you instantiate an instance if you do not not need to do anything initially for component. Would it be just:

def __init__():
    teddies[0].id = 0
    teddies[0].year = 2019
  • Did you try it? If so, did it work as expected? If not, what went wrong? Did you see an error message? Or did the contract not behave as you intended? – smarx Sep 1 at 5:42
  • I tried compiling my code but it does not compile. When I took out the body of init function it still does not compile in the remix compiler. – Ethlearner Sep 1 at 6:10
3

The below code compiles at vyper.online. I had to drop the private and add self. to your accesses of the state variable teddies:

struct Teddy:
    id: uint256
    year: uint256
    component: map(uint256, uint256)

teddies: map(uint256, Teddy)

@public
def __init__():
    self.teddies[0].id = 0
    self.teddies[0].year = 2019

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.