3

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
2
  • 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?
    – user19510
    Sep 1, 2019 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, 2019 at 6:10

1 Answer 1

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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