Skip to content

Instantly share code, notes, and snippets.

@naddison36
Last active May 8, 2016 21:44
Show Gist options
  • Select an option

  • Save naddison36/cf811f1da0562beb350f70f187982e72 to your computer and use it in GitHub Desktop.

Select an option

Save naddison36/cf811f1da0562beb350f70f187982e72 to your computer and use it in GitHub Desktop.
== operator for enum's in Solidity
contract Card
{
enum Suit {Heart, Diamond, Shape, Club, Jocker}
Suit public suit;
enum Rank {Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace, Jocker}
Rank public rank;
function equal(Card card) returns (bool)
{
return suit == card.suit && rank == card.rank;
}
}
@naddison36
Copy link
Copy Markdown
Author

The above is failing with the following error

Card.sol:11:16: Error: Operator == not compatible with types enum Suit and function () returns (enum Suit)
        return suit == card.suit && rank == card.rank;
               ^---------------^
Card.sol:11:37: Error: Operator == not compatible with types enum Rank and function () returns (enum Rank)
        return suit == card.suit && rank == card.rank;

@naddison36
Copy link
Copy Markdown
Author

parenthesis are needed when referencing an enum variable as it is a function. The following code compiles

contract Card
{
    enum Suit {Heart, Diamond, Shape, Club, Jocker}
    Suit public suit;

    enum Rank {Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace, Jocker}
    Rank public rank;

    function equal(Card card) returns (bool)
    {
        return suit == card.suit() && rank == card.rank();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment