Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: specification
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      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

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

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: