1

I read that enums were not allowed as keys in mappings, but when I just tried it it is working on remix.ethereum.org. Was it allowed in more recent Solidity versions?

The use-case I am using enum as key for is to encode tokens a bit differently, for a contract with multiple tokens. So it really makes that easier to read with enum Token as the key.

enum Token { TokenName1, TokenName2, TokenName3, TokenName4 }

mapping (Token => mapping (address => uint)) balanceOf;
mapping (Token => mapping (address => mapping (address => uint))) public allowed;
11
  • So what kind of answer exactly are you hoping to get here? It's either a 'yes' or a 'no', and all you really need to do in order to find out is try... which you already have! Sep 19, 2020 at 15:24
  • 1
    Anyone could go into as much detail as they like, "yes it was made possible then and then, not possible to begin with for reason this or that, etc". As far as I knew, IDE could have an error and it is still not formally possible. But I Googled a bit more now and this is a reference to that enum as keys were now allowed. github.com/ethereum/solidity/issues/1409. A lot of sources said it wasn't possible, like the docs here, solidity.readthedocs.io/en/v0.5.3/types.html, this question here as good a way as any to communicate that enums are now allowed as keys. Whatever people want.
    – Johan
    Sep 19, 2020 at 16:21
  • "Was it allowed in more recent Solidity versions?" "Yes it was added 6 months ago in v. this or that" for example. Whatever anyone wants. Or the question can be ignored and there is the downvote mechanism to help with that.
    – Johan
    Sep 19, 2020 at 16:25
  • I don't think there's any reason to downvote it because there's nothing wrong with it per se. What I meant to say was that other than a preliminary research (which you seemed to have already done), there was nothing left to add on top of that. Sep 19, 2020 at 18:07
  • I'm not a computer scientist so my ability to reason around if the IDE could have been wrong, etc, is probably less than yours. I guess if it runs in Remix, it means the code is being compiled, which means it is able to interpret the enum as a key and that it would not have worked before (why enums were not allowed to begin with. ) And that that is proof and answers my question.
    – Johan
    Sep 19, 2020 at 18:59

1 Answer 1

1

Enum as keys in a mapping were added in Solidity v. 0.6.3, released February 18 2020. This document seems to cover the history of Solidity language, https://github.com/ethereum/solidity/blob/develop/Changelog.md, scroll to 0.6.3.

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.