Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum -> enum class #180

Open
Sable-20 opened this issue Sep 27, 2023 · 1 comment
Open

enum -> enum class #180

Sable-20 opened this issue Sep 27, 2023 · 1 comment
Labels
enhancement Making the engine better in terms of code/optimization etc.

Comments

@Sable-20
Copy link
Contributor

What
Consider changing all enums to enum classes, this produces less surprises in code as things aren't implicitly converted to types

Why
Allows less surprises in code. See example below

enum Color { red, green, blue };                    // plain enum 
enum Card { red_card, green_card, yellow_card };    // another plain enum 
enum class Animal { dog, deer, cat, bird, human };  // enum class
enum class Mammal { kangaroo, deer, human };        // another enum class

void fun() {

    // examples of bad use of plain enums:
    Color color = Color::red;
    Card card = Card::green_card;

    int num = color;    // no problem

    if (color == Card::red_card) // no problem (bad)
        cout << "bad" << endl;

    if (card == Color::green)   // no problem (bad)
        cout << "bad" << endl;

    // examples of good use of enum classes (safe)
    Animal a = Animal::deer;
    Mammal m = Mammal::deer;

    int num2 = a;   // error
    if (m == a)         // error (good)
        cout << "bad" << endl;

    if (a == Mammal::deer) // error (good)
        cout << "bad" << endl;

}

Extra information: (optional)
https://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum

@Sable-20 Sable-20 added the enhancement Making the engine better in terms of code/optimization etc. label Sep 27, 2023
@aryanbaburajan
Copy link
Member

That's interesting, never knew about it until now, thanks for pointing it out. I'll ensure enum class is used in the upcoming rewrite that is yet to be pushed upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Making the engine better in terms of code/optimization etc.
Projects
None yet
Development

No branches or pull requests

2 participants