Why is Java's type inference so weak?

siledh

Say, I have a method:

public static <T> Collection<T> addToCollection(T element, Collection<T> collection) {
    collection.add(element);
    return collection;
}

And then when trying to compile this code:

Integer i = 42;
Collection<Integer> result = addToCollection(i, Collections.emptyList());

I get an error Type mismatch: cannot convert from Collection<Object> to Collection<Integer>. Could anyone explain why the type system cannot infer that Collections.emptyList() should be of type Collection<Integer>?

The example above is obviously quite artificial, but I stumble upon that limitation all the time and it's really annoying. After having read Effective Java I have found out that you can simply do Collections.<Integer>emptyList() (must say, that was quite a revelation for me at the time) and have everything compiling smoothly, but when you have some complicated type then it really is a nuisance.

I'm just wondering if this is some sort of bug, or are there any valid reasons for it to work that way?

assylias

The type inference system has been improved in Java 8, with the introduction of target typing, in order to give more expressivity to streams and lambdas. As a consequence your code compiles with Java 8.

More about it in the updated tutorial, with a very similar example towards the very bottom of the page.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does type inference in mutable and immutable sets behave so differently?

From Dev

Java type inference with erasure

From Dev

Type inference in java

From Dev

Why does the Java 8 generic type inference pick this overload?

From Dev

Why does Java generics type inference break in chained method calls?

From Dev

How type inference works in Java or why mockito even compile

From Dev

Java 10: Will Java 7's Diamond Inference Work with Local Type Inference?

From Dev

Scala type inference for both a generic type and it's type parameter - why doesn't it work?

From Dev

Java var and inference type ambiguity

From Dev

Java Type Inference in Static Methods

From Dev

Java Generics, Type Inference, Inheritance?

From Dev

Generic type inference limits in Java

From Dev

Java var and inference type ambiguity

From Dev

Scala: type inference of generic and it's type argument

From Dev

Java type inference of generic exception type

From Dev

Why the inference engine need explicit type with currying

From Dev

Why getOrElse would lose type inference in scalaz

From Dev

Why is this type inference not working with this Lambda expression scenario?

From Dev

Why doesn't type inference work in this case

From Dev

Why is type inference impractical for object oriented languages?

From Dev

why does type inference break with SelectedItemCollection?

From Dev

Why does type inference fail here?

From Dev

Why is type inference impractical for object oriented languages?

From Dev

Why getOrElse would lose type inference in scalaz

From Dev

Why Doesn't Java 8 Type Inference Consider Exceptions Thrown by Lambdas in Overload Selection?

From Dev

Why didn't this java 8 example using type inference compile in Eclipse?

From Dev

Why Java type inference fails in constructor for Collection of int array e.g. PriorityQueue<int[]>?

From Dev

Why Java type inference fails in constructor for Collection of int array e.g. PriorityQueue<int[]>?

From Dev

Why doesn't type inference work the same on lambdas and method references in Java?

Related Related

  1. 1

    Why does type inference in mutable and immutable sets behave so differently?

  2. 2

    Java type inference with erasure

  3. 3

    Type inference in java

  4. 4

    Why does the Java 8 generic type inference pick this overload?

  5. 5

    Why does Java generics type inference break in chained method calls?

  6. 6

    How type inference works in Java or why mockito even compile

  7. 7

    Java 10: Will Java 7's Diamond Inference Work with Local Type Inference?

  8. 8

    Scala type inference for both a generic type and it's type parameter - why doesn't it work?

  9. 9

    Java var and inference type ambiguity

  10. 10

    Java Type Inference in Static Methods

  11. 11

    Java Generics, Type Inference, Inheritance?

  12. 12

    Generic type inference limits in Java

  13. 13

    Java var and inference type ambiguity

  14. 14

    Scala: type inference of generic and it's type argument

  15. 15

    Java type inference of generic exception type

  16. 16

    Why the inference engine need explicit type with currying

  17. 17

    Why getOrElse would lose type inference in scalaz

  18. 18

    Why is this type inference not working with this Lambda expression scenario?

  19. 19

    Why doesn't type inference work in this case

  20. 20

    Why is type inference impractical for object oriented languages?

  21. 21

    why does type inference break with SelectedItemCollection?

  22. 22

    Why does type inference fail here?

  23. 23

    Why is type inference impractical for object oriented languages?

  24. 24

    Why getOrElse would lose type inference in scalaz

  25. 25

    Why Doesn't Java 8 Type Inference Consider Exceptions Thrown by Lambdas in Overload Selection?

  26. 26

    Why didn't this java 8 example using type inference compile in Eclipse?

  27. 27

    Why Java type inference fails in constructor for Collection of int array e.g. PriorityQueue<int[]>?

  28. 28

    Why Java type inference fails in constructor for Collection of int array e.g. PriorityQueue<int[]>?

  29. 29

    Why doesn't type inference work the same on lambdas and method references in Java?

HotTag

Archive