10

Suppose you have a generic interface:

public interface MyInterface<T> {
    T doSomething();
}

Is it possible to declare an enum that implements MyInterface<T>, but for which which every enum constant implements it for a different value of T? That is, given this enum:

public enum MyEnum {
    FOO,
    BAR,
    BAZ;
}

can we change it so that FOO implements MyInterface<Integer>, BAR implements MyInterface<String>, and BAZ implements MyInterface<List<MyOtherType>>, and make it so that MyEnum overall implements MyInterface<?>? It seems entirely feasible doing this raw, so it may be the case that it can be done in a typesafe manner.

zb'
  • 8,071
  • 4
  • 41
  • 68
Kelvin Chung
  • 1,327
  • 1
  • 11
  • 23

2 Answers2

12

No, as amalloy pointed out, Java doesn't allow enums to be declared with type parameters. It becomes clear why if you think about the way enums are meant to be used, for example in a switch.

Also consider how the language would implement generic enums - it isn't trivial. For a generic enum MyEnum<T>, each enum constant would need to resolve T to some specific type, otherwise they wouldn't be constants at all. Consider this:

enum MyEnum<T> {
    FOO; // T is not resolved
}

What is T for FOO here? The language would need a new syntax just to be able to express it, for example:

enum MyEnum<T> {
    FOO<String>;
}

So now we're getting into added complexity for the language in order to support semantics that don't have an overly compelling use case. It's easy to see why the language designers would have simply nixed type parameters for enums.

The workaround:

You can emulate your desired pattern by simply not using an enum. Organize the implementations of your interface into a utility class:

public class MyImplementations {

    public static final MyInterface<Integer> FOO =
            new MyInterface<Integer>() {
                ...
            };

    public static final MyInterface<String> BAR =
            new MyInterface<String>() {
                ...
            };

    public static final MyInterface<List<MyOtherType>> BAZ =
            new MyInterface<List<MyOtherType>>() {
                ...
            };

    private MyImplementations() { }
}

The only thing inherently missing is a way to iterate over the different implementations, as you could have done with MyEnum.values() - but with a hypothetical MyEnum<T>, the most specific type you could iterate over would be MyEnum<?>.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • This is probably best answer around with alternatives and explanation why is not possible – Tomo Oct 06 '14 at 22:12
4

No. How could someone use such a class?

MyEnum x = whatever;
x.foo().get(0); // how can javac know that x is the version that gives back a List?
amalloy
  • 89,153
  • 8
  • 140
  • 205
  • 1
    Using my example above, I would have assumed that javac would be aware that something explicit such as `MyEnum.FOO.doSomething()` would return Integer, while an expression such as yours above (say, if `x` was a parameter passed into a method) would be a compile-time error, as `MyEnum` itself would implement `MyInterface>`, and `get()` is not a method in `Object`. All in all, it does seem contrary to the notion that the type of an enum constant is a final singleton subclasses of the enum class itself. – Kelvin Chung Nov 03 '12 at 07:40