Forum Stats

  • 3,851,934 Users
  • 2,264,053 Discussions
  • 7,904,909 Comments

Discussions

Type parameter inference for constructors

843793
843793 Member Posts: 41,732 Green Ribbon
edited May 1, 2003 6:03PM in Generics
JSR-14 allows the actual type parameter of a parameterized methode to be inferred by the compiler. I would like to suggest to extend this to constructor invocations. Very often the type parameter of a class can be determined by the arguments of a constructor. I'm using generics a lot and to enable this I create static "construct" methods that do actually do this.

An example:
public class Pair<F, S> {

  public static <F1, S1> Pair<F1, S1> construct(F1 first, S1 second) {
    return new Pair<F1, S1>(first, second);
  }

  private F _first;
  private S _second;

  public Pair(F first, S second) {
    super();
    _first  = first;
    _second = second;
  }

  ...
}
The Pair.construct method just invokes the constructor. By using Pair.construct, the value of the F and S parameters can be inferred by the compiler. This saves a lot of code when constructing instances of parameterized classes.

Can you think of any problems?

Comments

  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    Well, at the moment, with invariant subtyping of parameterized classes, it may well turn out the type inference will not compute the parameterization you "need" for a class. For example, if you pass an Integer and a String to the constructor, you will always get Pair<Integer,String> back --- even if what you wanted was Pair<Number,Comparable>. Since you can't cast Pair<Integer,String> to Pair<Number,Comparable> this is a problem. Better to specify what you want explicitly.

    However, if invariant subtyping is not your only solution, than type inference may become more useful. It should be noted, however, that by and large Java is not a type-inferred language: types are required to be explicitly specified on fields, variables, method parameters, etc. It's very consistent with the rest of the language to require explicit types on object creation. The type inference mechanism for generic method invocation, although quite necessary to keep generic types from ballooning the code, is not actually a very good fit with the rest of the language design.
  • 843793
    843793 Member Posts: 41,732 Green Ribbon
    it may well turn out the type inference will not compute the parameterization you "need" for a class. For example, if you pass an Integer and a String to the constructor, you will always get Pair<Integer,String> back --- even if what you wanted was Pair<Number,Comparable>.

    Therefore it should still be allowed to specify the parameterization by yourself. This is actually required when parameters cannot be determined from the arguments of the constructor. This behaviour should be exactly the same as the type inference for parameterized methods.

    It should be noted, however, that by and large Java is not a type-inferred language

    I agree, but I like it a lot in the case of parameterized methods ;) . It would be horrible to specify all the type parameters.
This discussion has been closed.