Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java switch

Name: Anonymous 2011-10-20 11:32

Why does java not allow String objects to be used in the switch/case statements? Couldn't the compile invoke hashcode() on those strings thus producing a unique integer value to represent the constant in the cases?

Name: Anonymous 2011-10-21 21:50

>>41

Thanks, using the or is a lot more succinct and easy to read than the metatable way I used, and factoring the values out is nice. I only included the values inside of the functions to show that the functions had the ability to access and mutate variables in the outer function's local scope. This is a lot more difficult to pull off in Java. You can get lexical closures for final variables, which would let the case functions access values in the outer scope (which you would have to assign into final variables), but they would not be able to mutate them. So you would either have to take advantage of their return values somehow, or pass in references to objects for them to operate on. It's never really a switch and case statement, but it could still be useful for some things, but it would never be a convenient syntactical structure. It could instead be something that is constructed once, and then passed around and used.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List