Why can javac infer generic type parameters of functions used as parameters?

In the following example, why can the compiler infer that in foo Foo. 0 is called for the first time in test() A generic parameter to create (), but not in the second example? I'm using java 6@ H_ 403_ 2@public class Nonsense { public static class Bar { private static void func(Foo
arg) { } } public static class Foo

{ public static

Foo

create() { return new Foo

(); } private static void test() { Foo

foo2 = Foo.create(); // compiles Bar.func(Foo.create()); // won't compile Bar.func(Foo.

create()); // fixes the prev line } } }






(compilation error is that func (nonsense. Foo) method of nonsense.bar type is not applicable to parameter (nonsense. Foo))

Note: I understand that compiler errors can be fixed by the third line in test () - I wonder if there is a specific restriction that prevents the compiler from inferring types In my opinion, there is enough context here

Solution @ h_ 419_ 10 @ starting from Java 7, the resolution of method overloading must be carried out before any target type information of the method you call is considered, so as to infer the type variable t in the declaration of func. This seems stupid, because we can see that in this case, there is only one method named func. However, it is enforced by JLS and is the behavior of javac in Java 7 The compilation is as follows: first, the compiler sees that it is compiling the call of a static method of class bar named func To perform an overload resolution, it must find the parameters that call the method Although this is a trivial situation, it must still do so until it does, and it has no information about the formal parameters of the methods that can be used to help it The actual parameters include one parameter, which calls foo Create(), which is declared to return foo < T > Again, there is no standard from the target method, it can only infer that the return type is foo < T > This is foo < Object >, which does this Therefore, the method overload resolution failed because any overload of func is not compatible with the actual parameters of foo < Object >, and an error is issued Of course, this is very unfortunate. If the information can simply flow out from the other direction, from the target callback of the method to the calling site, the type can be easily inferred without error In fact, the compiler in Java 8 can do this, and As another answer says, this richer type inference is very useful for Lambdas added in Java 8 and extensions of Java APIs that are using Lambdas You can download the pre - release version of Java 8 with JSR 335 Lambdas from the above link It compiles the code in the problem without any warnings or errors@ H_ 403_ 8@ @H_ 403_ 8@ summary the above is collected by programming house for you. Why can javac infer generic type parameters of functions used as parameters? I hope this article can help you solve why javac can infer generic type parameters of functions used as parameters? Program development problems encountered. If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends. This graphic content is collected and provided by netizens on the Internet as a learning reference. The copyright belongs to the original author. Like to share programming technology and work experience with others. Welcome to join the official exchange group of programming house! Programming House official 1 group programming House official 2 group programming House official 3 group programming House official 4 group one: how to use monkeyrunner API to make Java next: Java - index and search date in Lucene

Solution

Starting from Java 7, the resolution of method overloading must be carried out before any target type information of the method you call is considered, so as to infer the type variable t in the declaration of func. This seems stupid, because we can see that in this case, there is only one method named func. However, it is enforced by JLS and is the behavior of javac in Java 7

The compilation is as follows: first, the compiler sees that it is compiling the call of a static method of class bar named func To perform an overload resolution, it must find the parameters that call the method Although this is a trivial situation, it must still do so until it does, and it has no information about the formal parameters of the methods that can be used to help it The actual parameters include one parameter, which calls foo Create(), which is declared to return foo < T > Again, there is no standard from the target method, it can only infer that the return type is foo < T > This is foo < Object >, which does this

Therefore, the method overload resolution failed because any overload of func is not compatible with the actual parameters of foo < Object >, and an error is issued

Of course, this is very unfortunate. If the information can simply flow out from the other direction, from the target callback of the method to the calling site, the type can be easily inferred without error In fact, the compiler in Java 8 can do this, and As another answer says, this richer type inference is very useful for Lambdas added in Java 8 and extensions of Java APIs that are using Lambdas

You can download the pre - release version of Java 8 with JSR 335 Lambdas from the above link It compiles the code in the problem without any warnings or errors

The above is collected by programming house for you. Why can javac infer generic type parameters of functions used as parameters? I hope this article can help you solve why javac can infer generic type parameters of functions used as parameters? Program development problems encountered.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>