Target types enum tests: scope enum constants in pattern matching expressions when the left han operator is an enum #8903
-
When doing pattern matching with enum values it could save some code if we didn't need to repeat the enum type in the expression since that's already known to the compiler. Take the code for the current version of the language: enum E
{
A = 1;
B = 2;
C = 3;
}
public string M(E value) => value is E.A or E.C ? "A" : "C"; What would perhaps be more pleasant is if the enum constants were scoped to the right hand expression if the left hand expression is an enum. public string M(E value) => value is A or C ? "Eff" : "Oof"; The reason I came over this is because I'm working with gRPC and the enum checks becomes quite a mouthful. if (request.NewState is VesselTrackingStateRequest.Types.VesselTrackingStatus.Undefined)
{
// ...
} Maybe this could just use if (request.NewState is Undefined)
{
// ...
} QuestionsShould this be allowed in normal equality operators? if(myEnum == A) { } Maybe it could more generally be a target type expression, and not just for comparisons? void M1(E value) { /* ... */ }
void M2()
{
E myValue = A;
M1(A);
} Or it could just allow static members of a type that is of that same type in order to allow things like this:
|
Beta Was this translation helpful? Give feedback.
#8641 #4479