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

Tweak return type substitutability

XMLWordPrintable

    • generic
    • generic

      ###@###.### writes:

      class Super {
         public <T> T get() { ... }
      }
      class Sub extends Super {
         public <T extends Number> T get() { ... } // overloads
      }

      And some invocations:

      Sub sub = new Sub();
      String s1 = sub.get(); // (1) error: ambiguous method call
      Number n1 = sub.get(); // (2) error: ambiguous method call
      String s2 = sub.<String>get(); // (3) Super.<T>get() with T:=String
      Number n2 = sub.<Integer>get(); // (4) error: ambiguous method call

      The ones that puzzle me are (1) and (3). Why are they different?

      I do not pretend that I understand section 15.12 of the JLS, but from
      Section 15.12.2.2 (Phase 1: Identify Matching Arity Methods Applicable
      by Subtyping) I thought I understood that very early in the resolution
      process you pick as the type argument of a generic method either the
      explicit type argument or the result of type inference.

      In case (1) I would expect that type inference yields T:=String. Hence
      I would expect (1) and (3) are treated the same. No? Why not?

      ...


      Actually, giving it a second thought, I realized that I do not even
      understand why any of the calls is ambiguous in the first place. For
      instance (4). Isn't <T extends Number> T get() clearly more specific
      than <T> T get() when sub.<Integer>get() is called?

            abuckley Alex Buckley
            ahe Peter Ahe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: