Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

q: enums and switch  RSS feed

 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding of how enums 'live' and how they work with switch/case statements needs some help.

1) I though the switch argument was basically an integer, at least on a byte code level. So how does it work with enums then?

2) But ok, accepting that...why is an unqualified enum (SMALL) ok to use as a switch argument but not a qualified enum (Size.SMALL)?

Thanks in advance for expanding my wee little brain.

Stu
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) the switch syntax was expanded to enable using Enums.
2) Because it still requires it to be symbolic constants, and the full name technically isn't one.
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanks. but i'm still not 100% there.

What exactly is the def/spec of a symbolic constant in Java? I am OK with the abstract concept. My two minute google did not help.

If the qualified enumeration is not a symbolic constant then what is it?

tia,

stu
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Stu]: why is an unqualified enum (SMALL) ok to use as a switch argument but not a qualified enum (Size.SMALL)?

I think don't mean the switch argument - instead you're talking about the targets of the cases, right? E.g.

For the case targets, giving a fully qualified name is completely unnecessary since the compiler alread knows the type of the switch argument. And if we're talking about an enum, all the case targets must be of the same type as the switch argument. So the compiler infers the type of the targets from the argument. They chose to make it illegal to redundantly specify the type again in the targets, just to prevent unnecessary verbosity (and eliminate the possibility of the programmer accdentally getting it wrong). They do that sort of thing sometimes.
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

I think don't mean the switch argument - instead you're talking about the targets of the cases, right? E.g.

Ooops...my bad...you interpreted my question correctly Thanks.

OK, I see the logic behind not wanting a programmer to accidentally use the incorrect type. I was looking for some sort of consistency that now does not make sense in this context. (Don't ask what sort )

Thanks again,

Stu
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!