1 2 Previous Next 16 Replies Latest reply on Sep 1, 2010 12:46 PM by 843793 Go to original post
      • 15. Re: Parameterized enum Instantation
        I have wanted something similar myself, and have exhausted all avenues my perverted imagination can conceive of. (And when it comes to generics there are plenty of avenues to pervert)

        You can do it using the type safe enum pattern, but not as an actual enum.

        But be careful that you get serialization to work properly (if required) and all the other tricky stuff that enum just does right.

        (yes it would be nice to be able to extend Enum in order to inherit that, but you can't, at least you can't if you write your type safe enum in Java :) )

        • 16. Re: Parameterized enum Instantation
          One can actually convince javac with some bytecode weaving, the JDT will issue unchecked warnings though. The reason I ultimately decided it was useless (the JDT can be patched afterall) is that Java can not locally deduce the actual type arguments, i.e. there will still be unchecked casts:
          enum Blub<T> {
            FOO<String>, Bar<Integer>
          public static <T> T test(Blub<T> blub) {
            switch (blub) {
              case FOO:
                return (T) "frobb"; // unchecked cast
              case BAR:
                return (T) Integer.valueOf(42); // unchecked cast
                throw new AssertionError();
          With kind regards
          1 2 Previous Next