0

I have a mapping: mapping(uint256 => uint256[]) public derivedTokens; In some cases, I want to define the size of the uint256[] dynamic array to 5. But if I try this: derivedTokens[tokenId] = uint256[5]; I get the following error: from solidity:

TypeError: Type type(uint256[5] memory) is not implicitly convertible to expected type uint256[] storage ref.

How can I resolve this issue?

PS: Another question would be "How to have different fixed sized arrays within a mapping?" or is this possible?

1 Answer 1

0

Your syntax is wrong given your value type of uint256[], try like this :

// Create a DYNAMIC array with length 5
derivedTokens[tokenId] = new uint256[](5);

Keep in mind that those are not fixed arrays but dynamic ones, it's possible to push / pop them and therefore modify the size.

PS: Another question would be "How to have different fixed sized arrays within a mapping?" or is this possible?

You can handle storage however you see fit, so it's technically possible, but doing so is hacky and not recommended. The compiler expect the value type of the mapping to be unique (i.e., every value has the same type). So there is no simple way of doing it.

2
  • If these are dynamic arrays then what does 5 do?
    – MehmedB
    Apr 22, 2022 at 12:53
  • It just initializes the length of the array to 5. You could put the value of your choice here, it's just a way to set the initial length.
    – hroussille
    Apr 22, 2022 at 13:09

Your Answer

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

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