Honestly I don't see the reason for putting non-constant value in enums and it looks like antipattern to me. Can you give an example where that would make sense? I'm genuinely interested.
An obvious use case is expressions that optimize to constants during compilation, as in the bitwise OR of two values example above, because it makes the source code clearer without compromising what enumerations bring to programming.
Perhaps a given scenario where the non-constant value can easily be computed? There's probably a better way of handling that kind of scenario that doesn't involve a enum; I can't imagine using a non-constant value is considered best-practice.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Honestly I don't see the reason for putting non-constant value in enums and it looks like antipattern to me. Can you give an example where that would make sense? I'm genuinely interested.
An obvious use case is expressions that optimize to constants during compilation, as in the bitwise OR of two values example above, because it makes the source code clearer without compromising what enumerations bring to programming.
Thanks. That reminds me of the
constexpr
in C++.Perhaps a given scenario where the non-constant value can easily be computed? There's probably a better way of handling that kind of scenario that doesn't involve a enum; I can't imagine using a non-constant value is considered best-practice.