Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6408723

Parameterized enum type

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Not an Issue
    • P4
    • None
    • 6
    • specification
    • x86
    • linux, windows_vista

    Description

      A DESCRIPTION OF THE REQUEST :
      Enums do not take type parameters.
      The following code doesn't compile:

      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");
          }
        }
      }

      As well as this code:
      public enum UserInfo<T> {
        FirstName<String>,
        LastName<String>,
        UserSince<Date>,
        LastLogin<Date>;
      }

      public <T> void setUserInfo( UserInfo<T> infoType, T infoValue ) {}


      JUSTIFICATION :
      Allowing enums to be parameterized allows writing type-safe methods that enforce compile-time consistency between their parameters;
      for example, the method:
      public <T> void setUserInfo( UserInfo<T> infoType, T infoValue ) {}
      which would compile when called on (FirstName, "Mathew"), but issue an unchecked warning or refuse to compile when called with (UserSince, "31/07/2004").

      This allows code to be written in a type-safe way when it was impossible before.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code above compiles without warnings.
      ACTUAL -
      The above code doesn't compile; the type parameter is rejected by the compiler.

      Attachments

        Issue Links

          Activity

            People

              abuckley Alex Buckley
              rmandalasunw Ranjith Mandala (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: