3

A bit of context. This is about the Problem with qualified enum names in switch cases like in the example:

enum MyEnum {
    A,
    B,
    ;
}
switch(methodReturnungMyEnum()){
case MyEnum.A:
    // ...
    break
case MyEnum.B:
    // ...
    break
}

which yields the compiler error

An enum switch case label must be the unqualified name of an enumeration constant

Yeah. The solution is simple: Remove MyEnum. part. Thats not my question.

I was simply wondering why this is forbidden in the first place. I know that it is basically impossible to definitely answer why something was done in a certain way. Instead i want to ask for reasons that might have caused this decision. In what way are qualified and unqualified enum constants (or maybe symbols in general) different? And what could go wrong if the compiler would allow this anyway?

While there exists quite a few questions on how to fix the compiler error itself nobody seems to address above questions.

Burdui
  • 1,242
  • 2
  • 10
  • 24
  • The compiler *knows* that it's selecting a case based on the `MyEnum` enumeration, so there's no need to fully qualify the constant. The error message could maybe be worded a little better. – Some programmer dude Aug 17 '21 at 08:32
  • 1
    @MuratKaragöz that answer does not explain the decission why can not (only technically why can not). – josejuan Aug 17 '21 at 08:37
  • Relevant: [1](https://meta.stackoverflow.com/a/293819/5133585) and [2](https://meta.stackoverflow.com/a/323382/5133585). – Sweeper Aug 17 '21 at 08:40
  • @Sweeper here exists a technical reason to not enable that feature (qualify the constant) and is explained in my answer. I'm answering showing the cost of do it (that is objetive). Do you really not think it is useful for people to learn about this concept that they probably wonder about frequently? – josejuan Aug 17 '21 at 08:43
  • @josejuan I never said that there is no technical reason, and I never said that it is not useful to learn about this concept. My point is just that this question is poorly asked. I quote: "The fundamental problem with the 'why' question is not that the answer is an opinion. The fundamental problem is that it is impossible to know what will satisfy the person asking the question because the question is vague" – Sweeper Aug 17 '21 at 08:58
  • (mmm ok but is clear and perfectly specific for me) – josejuan Aug 17 '21 at 09:00

1 Answers1

1

The real enumeration type is defined into the switch sentence then, for every case clause the compiler only must to check the literal exists into that enumeration.

If you allow to specify the qualified (full qualified, etc...) in the case clause then, the compiler must to perform a useless step (check that symbol is a member of the given enumeration).

That is the reason why it is not the same to put it as not (which is what it might seem at first).

Technically that restriction is responsed here and explained here.

josejuan
  • 9,338
  • 24
  • 31
  • 1
    Thanks. So to conclude: there is no technical limitation but it would be an additional specification and implementation step that has not been taken. – Burdui Aug 17 '21 at 08:46