8

I can't get comparisons (==) of enum types compiling. The following example

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;
    }
}

has the following compile errors

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;

Any ideas on how variables of an enum type can be compared?

Thanks Nick

  • 4
    I believe card.suit is a function, while card.suit() should be the enum. – Karl Floersch May 8 '16 at 20:35
5

Thanks, Karl Floersch. card.suit() does return the enum. The following code now 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();
    }
}
| improve this answer | |
0

I believe you need the stringUtils.sol library which gives you access to comparing strings.

/// @dev Compares two strings and returns true iff they are equal.
    function equal(string _a, string _b) returns (bool) {
        return compare(_a, _b) == 0;
    }
| improve this answer | |
  • 1
    how can I convert enum values to string – SwapnilKumbhar Jul 31 '17 at 7:47
  • @SwapnilKumbhar Please ask in a new question. – tayvano Aug 5 '17 at 2:04

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.