Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 and Generalized Target-Type Inference

I have installed the last JDK 8 ea b114 to test the new language features. However the inference in chained calls seems not to work yet.

If I write:

Iterator<String> it = new ArrayList<>().iterator();

the compiler give me an error.

However inference in argument position works well.

Maybe inference in chained calls will not be inserted?

like image 344
xdevel2000 Avatar asked Nov 12 '13 16:11

xdevel2000


People also ask

What is type inference in Java 8?

Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable.

Does Java 7 support type inference?

Java SE 7 supports limited type inference for generic instance creation; you can only use type inference if the parameterized type of the constructor is obvious from the context.

What is target type Java?

The Target-Type of an expression is the data type that the Java Compiler expects depending on where the expression appears. Java 8 supports inference using Target-Type in a method context.

What is infer generic type arguments?

Type inference represents the Java compiler's ability to look at a method invocation and its corresponding declaration to check and determine the type argument(s). The inference algorithm checks the types of the arguments and, if available, assigned type is returned.


2 Answers

As @Holger said, Java 8 improved contextual inference so that this works_

public static <T> Iterator<T> iter(Iterable<T> i)
{
    return i.iterator();
}

public static void main(String[] args)
{
    Iterator<String> it = iter( new ArrayList<>() );
                \____________________________/
}

It didn't work in Java 7 — the inference on new ArrayList<>() could not depend on context.


It'll be a huge change to the language to do what you want in the question. John Rose started a similar discussion, see http://mail.openjdk.java.net/pipermail/lambda-dev/2013-July/010531.html


Too much inference and too much contextual dependency can be a bad thing. It's not so much that the compiler cannot handle the complexity - it can. It's about whether human programmers can handle it. I am sensing that Java 8 is already at a dangerous level that the codes will be difficult for humans to parse.

like image 74
ZhongYu Avatar answered Sep 22 '22 21:09

ZhongYu


The latest specification (Public Review) is available at jcp.org. There is a paragraph in part D which discusses this point.

The receiver in a method invocation, field access, etc. (exp.foo()) is not a poly expression because the target type is unknown—it would be impossible to enumerate every type that has a particular member (foo, in this case). There has been some interest in allowing inference to "chain": in a().b(), passing type information from the invocation of b to the invocation of a. This adds another dimension to the complexity of the inference algorithm, as partial information has to pass in both directions; it only works when the erasure of the return type of a() is fixed for all instantiations (e.g. List). This feature would not fit very well into the poly expression model, since the target type cannot be easily derived.

like image 24
Ben Schulz Avatar answered Sep 23 '22 21:09

Ben Schulz