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

Static functions for enum types #100

Open
reddigfabian opened this issue May 3, 2021 · 1 comment
Open

Static functions for enum types #100

reddigfabian opened this issue May 3, 2021 · 1 comment

Comments

@reddigfabian
Copy link

I'm trying to emulate java's ability to add a static function to an enum class but using better enums.

`namespace playground {
BETTER_ENUM(Vegetable, int,
potato,
brocolli,
carrot
)

std::string getColor(Vegetable toGet) {
    switch (toGet) {
        case Vegetable::potato:
            return "brown";
        case Vegetable::brocolli:
            return "green";
        case Vegetable::carrot:
            return "orange";
        default:
            return "unknown";
    }
}

}`

I'd like to be able to simply call Vegetable::getColor(Vegetable::carrot), for example. Is there any way to add functions to the class generated by better enums?

@aantron
Copy link
Owner

aantron commented May 4, 2021

You may be able to use the default constructors override macro to either:

  • Give enums a public constructor, and derive a class from them.
  • Inject arbitrary code into the definition of an enum, because that's what the macro does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants