Reputation: 9801
Can anyone explain why the following error occurs (Scala 2.10.3)?
scala> new java.util.ArrayList[Integer]()
res0: java.util.ArrayList[Integer] = []
scala> res0.add(0)
res1: Boolean = true
scala> java.util.Collections.binarySearch(res0, 0)
<console>:9: error: type mismatch;
found : java.util.ArrayList[Integer]
required: java.util.List[_ <: Comparable[_ >: Any]]
java.util.Collections.binarySearch(res0, 0)
^
The following does work:
scala> java.util.Collections.binarySearch[Integer](res0, 0)
res4: Int = 0
It seems odd that the compiler would complain about a particular type until I was more explicit about that incorrect type and then it will accept it.
EDIT:
Also note the if you change the first step to:
scala> new java.util.ArrayList[Int]()
there is also a very similar error.
Upvotes: 1
Views: 1140
Reputation: 8487
Definition of binarySearch
is as follows:
Searches the specified list for the specified object using the binary search algorithm.
static <T> int
binarySearch(List<? extends Comparable<? super T>> list, T key)
As you can see, list
and key
are parameterized on a type T
. Now Scala compiler tries to infer the type of list
in following invocation:
scala> java.util.Collections.binarySearch(l, 0)
<console>:9: error: type mismatch;
found : java.util.ArrayList[Integer]
required: java.util.List[_ <: Comparable[_ >: Any]]
java.util.Collections.binarySearch(l, 0)
And the common type between the parameters list
and key
is Any
. Because former is of type List<Integer>
and later is of type int
as per Java. So this converts in Scala to Integer
and int
respectively, as explained below:
scala> classOf[Integer]
res9: Class[Integer] = class java.lang.Integer
scala> classOf[Int]
res10: Class[Int] = int
Upvotes: 0
Reputation: 32729
Try this:
java.util.Collections.binarySearch(res0, 0: Integer)
As you can see, it compiles fine.
The problem was that 0
has type Int
, not Integer
. So you have to tell scala somehow that you want to convert 0
into a Integer
value.
As it stands, your code triggers the compiler to look for a method binarySearch
which takes and ArrayList[Integer]
as its first parameter, and an Int
as its second parameter.
Upvotes: 8