Is there a way to enforce that you're switching over all defined values of an enum in Java? -
this question has answer here:
suppose have enum 3 values:
enum colors { red, green, blue }
you switch on values of in method, thinking you've handled cases:
switch (colors) { case red: ... case green: ... case blue: ... }
then later, add new value enum:
enum colors { red, green, blue, yellow }
and still compiles fine, except you're silently missing case yellow
in method. there way raise compile-time error in such scenario?
edit: not understand why marked dupe of can add , remove elements of enumeration @ runtime in java. since answer there "no", means should possible know @ compile-time values of enum, , therefore i'm asking should possible compiler/some code analysis tool implement, right?
that depends on compiler. eclipse ide built-in compiler can configured raise error in case.
windows/preferences java compiler errors/warnings "incomplete 'switch' cases on enum" can set "error".
edit:
there sub-option "signal if 'default' case exists".
Comments
Post a Comment