Re: Why Generics?




Dale King wrote:

> [../] the new enumerations are (in my
> > understanding) just a little syntactic sugar provided by the javac
> > compiler. So clearly type safe enums were always possible.
>
> There is actually one part of enums that was not possible before and
> that was the ability to use them as cases in a switch statement.

It was always possible, the new enums don't generate code that couldn't be
expressed in "normal" Java. Supporting switch would require a fairly
unpleasant amount of wordy duplication, though. You'd have to have a public
static final int field corresponding the the 'ordinal' of each enum object,
which is obviously error prone unless the compiler does it for you.

OTOH, if you use your own implementation of the type-safe enum pattern, then
their object-nature is not hidden by the compiler and so you can use
polymorphism to avoid having to use switch statements in the first place.

-- chris


.



Relevant Pages

  • Re: No call for Ada (was Re: Announcing new scripting/prototyping language)
    ... The article quoted first below is ... >In C enums are interchangable with ints so there is no error, ... >there maybe the compiler should give a warning. ... C99's aggregate constructors are probably ...
    (comp.lang.c)
  • Re: No call for Ada (was Re: Announcing new scripting/prototyping language)
    ... The article quoted first below is ... >In C enums are interchangable with ints so there is no error, ... >there maybe the compiler should give a warning. ... C99's aggregate constructors are probably ...
    (comp.lang.ada)
  • Re: new IL: C (sort of...).
    ... language terminated strings with a special character. ... "A Tour Through the Portable C Compiler" by S.C. Johnson ... actually, a bytecode construction API seems like a good idea, and would also ... it may be noted that my usual handling of enums is simply to replace them ...
    (comp.lang.misc)
  • Re: Enums in Java5 schlecht umgesetzt?
    ... selber eine Zahl, im gegensatz zu Java5, wo ein Element nur eine Zahl _hat_, beginnt bei 0, wird _immer_ automatisch vergeben). ... Wenn du also eine Abbildung von Enum-Element auf Zahlen machen willst, so hat das (aus Java Sicht) nichts mit Enums als solchen zu tun. ... aber sie könnten dem Compiler als Hint // dienen eine solche static Methode dann in der eigentlichen // Enum-Klasse zu generieren. ...
    (de.comp.lang.java)
  • Re: [take9 1/2] kevent: Core files.
    ... These are not seen by the C compiler. ... According to this one - no one wants to watch enums in debugger. ...
    (Linux-Kernel)