participate


Generics - parameterized enum types
<<   Back to Forum  |   Give us Feedback
This topic has 4 replies on 1 page.
langer
Posts:113
Registered: 6/12/97
parameterized enum types   
Aug 27, 2003 12:01 AM

 

Why can't we parameterize enum types? Is there a reason for the restriction?

I would find it natural to do something like the following:
public enum EnumType<T> {
    val1,val2,val3;
 
    private T attribute = null;
 
    public T getAttribute() { return attribute; }
    public void setAttribute(T t) { attribute = t; }
}


EnumType<List<String>> ref = EnumType<List<String>>.val2;
List<String> list = new LinkedList<String>();
... fill list ...
ref.setAttribute(list);


If I understand the enum proposal correctly then the enum type is basically a class that extends superclass Enum. Why can't that class be parameterized? Is there a technical reason for this restriction?



BTW, I would find it helpful if the enum spec mentioned that a nested enum type is implicitly static. Just for clarity; it aids understanding of certain error messages where the compiler talks of "static context".
class Outer {
  public enum Inner { ... }  // <<< this is a static nested type although I did not say so
}
 
langer
Posts:113
Registered: 6/12/97
Re: parameterized enum types   
Aug 27, 2003 12:35 AM (reply 1 of 4)  (In reply to original post )

 
I overlooked that the enum values are static fields of their own class. In that case the parameterization does not make sense. Too bad.
 
taochengde
Posts:3
Registered: 7/2/04
Re: parameterized enum types   
Jul 2, 2004 8:11 PM (reply 2 of 4)  (In reply to #1 )

 
Hi, I don't understand why the fact that enum values are static fields in their own class prevents parameterization. I'd really like to be able to do this:

public enum UserInfo<T> {
FirstName<String>,
LastName<String>,
UserSince<Date>,
LastLogin<Date>;
}

So that I can have compile time checking like this:

public <T> void setUserInfo( UserInfo<T> infoType, T infoValue ) {
// now someone would get a compile-time error if they called addData( FirstName, new Date() );
// which seems pretty neat.
}

I could basically duplicate the enum functionality myself and do it like this:
public class UserInfo<T> {
public static final UserInfo<String> FirstName = new UserInfo<String>();
public static final UserInfo<String> LastName = new UserInfo<String>();
public static final UserInfo<Date> UserSince = new UserInfo<Date>();
public static final UserInfo<Date> LastLogin = new UserInfo<Date>();
}

But now I've lost all the other enum features like being able to use switch, getting nice toString() methods, etc.

So I'm wondering since I can do the parameterized enum thing on my own, why isn't it supported by the enum construct?
 
Tobu
Posts:2
Registered: 4/4/06
Re: parameterized enum types   
Apr 4, 2006 6:19 AM (reply 3 of 4)  (In reply to original post )

 
This is a useful feature.
Here is what I want to do:
class Property<T> {
public abstract <T> T accept(Visitor<T> v);
public static enum Type<T> {
STRING<String>,
DOUBLE<Double>;
}
public static Property<T> newInstance(Type<T> type, T initValue) {
switch(type) {
case STRING:
return new StringProperty(initValue);
case DOUBLE:
return new DoubleProperty(initValue);
default:
throw new RuntimeException("invalid enum constant");
}
}
}

I see no obvious (language-wise) reason why this is forbidden.
 
Tobu
Posts:2
Registered: 4/4/06
Re: parameterized enum types   
Apr 6, 2006 1:32 AM (reply 4 of 4)  (In reply to #3 )

 
I've posted a bug for this, at:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6408723
Go vote for it!
http://bugs.sun.com/bugdatabase/addVote.do?bug_id=6408723
I used the example by taochengde too.
As langer pointed out, his problem is unrelated to enums (and the bug).
 
This topic has 4 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 53
  • Guests : 137

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums