Re: Why Generics?
- From: Phillip Lord <p.lord@xxxxxxxxxxxx>
- Date: 25 May 2005 18:54:29 +0100
>>>>> "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
.
- References:
- Why Generics?
- From: David Blickstein
- Re: Why Generics?
- From: Eric Sosman
- Re: Why Generics?
- From: Chris Uppal
- Re: Why Generics?
- From: Mike Schilling
- Re: Why Generics?
- From: Chris Uppal
- Re: Why Generics?
- From: David Blickstein
- Re: Why Generics?
- From: David Blickstein
- Re: Why Generics?
- From: Dale King
- Re: Why Generics?
- From: Chris Uppal
- Re: Why Generics?
- From: Phillip Lord
- Re: Why Generics?
- From: Chris Uppal
- Why Generics?
- Prev by Date: Re: ArrayOutOfBoundException when printing out a two dimension array
- Next by Date: Re: Why Generics?
- Previous by thread: Re: Why Generics?
- Next by thread: Re: Why Generics?
- Index(es):
Relevant Pages
|