Just out of curiosity, asking this
Like the expression one below
a = (condition) ? x : y; // two outputs
why can’t we have an operator for enums?
say,
myValue = f ??? fnApple() : fnMango() : fnOrange(); // no. of outputs specified in the enum definition
instead of switch statements (even though refactoring is possible)
enum Fruit
{
apple,
mango,
orange
};
Fruit f = Fruit.apple;
Or is it some kind of useless operator?
I can’t say I’ve ever wanted such an operator – which would be incredibly brittle by relying on the ordering of the enum values. You can easily use a switch:
Alternatively, create a map:
I’ve used both techniques in various situations, and far prefer them to the suggested operator, I’m afraid.