Question

I'm just starting out to learn Guava and I notice something peculiar with ImmutableList.builder().

This doesn't compile:

List<String> iList = ImmutableList.builder().add("One").add("Two").build(); 
//Type mismatch: cannot convert from List<Object> to List<String>

This works:

List<String> iList = new ImmutableList.Builder<String>().add("One").add("Two").build(); 

I can live with using new ImmutableList.Builder<String>() but is this a bug with ImmutableList.builder()?

Was it helpful?

Solution

No, just provide a type argument

List<String> iList = ImmutableList.<String>builder().add("One").add("Two").build(); 

This is in no way a bug in Guava, it is just a feature/limitation of the Java Language. The compiler cannot infer the return type of build() from the previous method calls or the declaration of the variable the result is being assigned to.

Angelika Langer explains this

Automatic type argument inference. The method is invoked like regular non-generic methods, that is, without specification of the type arguments. In this case the compiler automatically infers the type arguments from the invocation context.

She also gives a similar example in Why does the type inference for an instance creation expression fail?

String s = new ArrayList<>().iterator().next();  // error

and states

In the example above an error message is issued because the new-expression new ArrayList<>() does not have constructor arguments and it neither appears on the right-hand side of an assignment nor as the argument of a method invocation. Instead, it appears in a chain of method calls. Such a chain is not a valid type inference context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top