Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImmutableList.builder() bug?

Tags:

java

guava

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()?

like image 541
Rob Avatar asked Apr 01 '14 04:04

Rob


1 Answers

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.

like image 166
Sotirios Delimanolis Avatar answered Oct 18 '22 16:10

Sotirios Delimanolis