Re: Why Generics?



>>>>> "Chris" == Chris Uppal <chris.uppal@xxxxxxxxxxxxxxxxxxxxxxxxxxx> writes:

Chris> Phillip Lord wrote:

Chris> It was always possible, the new enums don't generate code
Chris> that couldn't be expressed in "normal" Java. Supporting
Chris> switch would require a fairly unpleasant amount of wordy
Chris> duplication, though. You'd have to have a public static
Chris> final int field corresponding the the 'ordinal' of each enum
Chris> object, which is obviously error prone unless the compiler
Chris> does it for you.
>>
>> I think, actually, you are wrong and it doesn't work. The problem
>> is that with switch case, the value of the cases must be a int or
>> char definable at compile time.

Chris> No, there's no problem except wordy, error-prone, repetition.

Chris> Suppose that we want an enumeration of the three primary
Chris> colours (this probably wouldn't compile but you'll be able to
Chris> see what I mean, I hope):

Chris> public final class Colour {
Chris> public static final int
Chris> __RED_ORDINAL = 0, __GREEN_ORDINAL = 1,
Chris> __BLUE_ORDINAL = 2;

Chris> and then a case statement can be written like:

Chris> public boolean isPutrid(Colour colour) {
Chris> switch (colour.ordinal()) { case
Chris> Colour.__RED_ORDINAL: case Colour.__BLUE_ORDINAL:
Chris> return false;
Chris> case Colour.__GREEN_ORDINAL:
Chris> return true;
Chris> default: // WTF ??
Chris> return false;
Chris> }
Chris> }

Chris> Which is almost exactly what the Java compiler does at the
Chris> bytecode level. Of course it would be a pain to maintain and
Chris> use (and not allow people to /ab/use) the __XXX_ORDINAL
Chris> numbers in parallel to the "real" enumeration objects --
Chris> that's what I meant when I called it wordy and error-prone,
Chris> but it isn't at all impossible.

Ah, okay, that would work.

The error prone bit is a serious problem.


Chris> Note that the implementation of switch is a /real/ switch (a
Chris> tableswitch bytecode instruction) which can be implemented as
Chris> a pure jump table. There is no cascade of if-thens, nor is
Chris> there any hashing going on.

Perhaps I have made a poor use of the word "hash".

I was sure that they implemented things with if-thens. Perhaps this is
what happened in an early version of the spec when I read it.

Chris> Actually, the bytecode generated by javac uses one more level
Chris> of indirection. The ordinal number is used to lookup an
Chris> integer value in a synthetic int[] array, and then the value
Chris> from the array is used in the generated switch statement. I'm
Chris> not sure why it does that. Possibly it's to allow switch
Chris> statements to continue to work if the enum definition is
Chris> changed after it has been compiled, though I don't really see
Chris> how that would work..


Hmm. That is strange.

Thanks for the information. Always good to learn more!

Phil
.



Relevant Pages

  • Re: Why Generics?
    ... Chris> It was always possible, the new enums don't generate code ... Chris> switch would require a fairly unpleasant amount of wordy ... Chris> final int field corresponding the the 'ordinal' of each enum ... which is obviously error prone unless the compiler ...
    (comp.lang.java.programmer)
  • Re: Why Generics?
    ... > Chris> It was always possible, the new enums don't generate code ... > that with switch case, the value of the cases must be a int or char ... No, there's no problem except wordy, error-prone, repetition. ... Which is almost exactly what the Java compiler does at the bytecode level. ...
    (comp.lang.java.programmer)
  • Re: Coldfusion vs. Java
    ... only Java programmer and the rest are developing in CF. ... Some of the management is trying to switch ... resisting programmers are right? ... Chris Smith - Lead Software Developer/Technical Trainer ...
    (comp.lang.java.programmer)
  • Re: Read Only Attribute Stuck ON
    ... Chris Wrote: ... and even if I take the check out and hit apply they switch ... Even new folders I just made are instently read ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: GTB tech help - does this make sense?
    ... Yup, what Chris said. ... Really, if you are going to maintain a game, you need the schematic. ... First check the outhole switch like Jim says, ... Look to see what switches must be closed to energize that coil. ...
    (rec.games.pinball)