Forum Stats

  • 3,851,386 Users
  • 2,263,969 Discussions
  • 7,904,692 Comments

Discussions

type parameter inference and conversions

843793
843793 Member Posts: 41,732 Green Ribbon
edited May 9, 2003 2:26PM in Generics
In this example:
public class X {
  private static <T> T f(T t) {
    return t;
  }
  public static void main(String[] args) {
    f(5); 
  }
}
Would the call to f be illegal? Or would it be legal and invoke
<Integer>f
and perform a boxing conversion on the method argument?

Basically it is the question regarding the impact of conversions on the inference process. What is the relationship between type parameter inference and method argument conversions? The spec does not say anything, or does it?

Comments

  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    The call to f would be illegal. Autoboxing is not supported.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon

    If the JSR 201 proposal will be accepted then Java will have support for autoboxing.

    Which raises the question: what is the relationship between autoboxing and type parameter inference? will autoboxing be treated as other conversions such as the widening reference conversions? what is the relationship between method argument conversion and type parameter inference?
                           public class Super {}
                           public class Sub extend Super {}
    
                           public class X {
                              private static <T> T f(T t) {
                                return t;
                              }
                              private static void g(Integer i) {}
                              private static Super h(Super s) { return s; }
    
                              public static void main(String[] args) {
                                f(5);   // 1
                                g(5);   // 2
                                Super r1 = f(new Sub());  // 3
                                Super r2 = h(new Sub());  // 4
                              }
                            }
    Call // 4 is legal. Is call // 3 legal? Which method does it call: <Sub>f or <Super>f ? In other word, does the call involve a widen reference conversion on the method argument?

    Call // 2 is legal (provided that autobxing makes it into the language). Is call // 1 legal? Does it call <Integer>f and performs an autoboxing conversion on the method argument? Or does type parameter inference infer 'int', in which case the call would be illegal?
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    Very good question. Fantastic example.

    Oh, I can't wait for the next drafts of these proposals!
This discussion has been closed.