L
Lasse Reichstein Nielsen
Hi all.
I was taking yet another look at generics, and noticed that enums cannot
be generic.
After all, the "enum" construct is syntactic sugar for the traditional
type safe enum pattern, and with generics, you could write something like:
---
class RandomConstant<T> {
public final RandomConstant<Integer> INT =
new RandomConstant<Integer>(new Integer(87));
public final RandomConstant<String> STRING =
new RandomConstant<String>("hello world");
private final Object v;
RandomConstant(T value) {
this.v = value;
}
public T value() {
return (T) v;
}
}
---
(yes, braind dead example
I was thinking that I should be able to write that with an enum,
something syntactically like this:
---
enum RandomConstant<T> {
INT<Integer>(new Integer(87)),
STRING<String>("hello world");
private final Object v;
private RandomConstant(T value) {
this.v = value;
}
public T value() {
return (T) value;
}
}
---
Alas, it isn't so. Does anybody know whether this is a deliberate choice,
and if so, what the reasoning behind it is (or am I just generalizing
too far as usual ?
Regards
/L
I was taking yet another look at generics, and noticed that enums cannot
be generic.
After all, the "enum" construct is syntactic sugar for the traditional
type safe enum pattern, and with generics, you could write something like:
---
class RandomConstant<T> {
public final RandomConstant<Integer> INT =
new RandomConstant<Integer>(new Integer(87));
public final RandomConstant<String> STRING =
new RandomConstant<String>("hello world");
private final Object v;
RandomConstant(T value) {
this.v = value;
}
public T value() {
return (T) v;
}
}
---
(yes, braind dead example
I was thinking that I should be able to write that with an enum,
something syntactically like this:
---
enum RandomConstant<T> {
INT<Integer>(new Integer(87)),
STRING<String>("hello world");
private final Object v;
private RandomConstant(T value) {
this.v = value;
}
public T value() {
return (T) value;
}
}
---
Alas, it isn't so. Does anybody know whether this is a deliberate choice,
and if so, what the reasoning behind it is (or am I just generalizing
too far as usual ?
Regards
/L