Alejandro Alcalde
Alejandro Alcalde

Reputation: 6220

Type parameter Bounds [T <: Comparable[_]] in guava's MinMaxPriorityQueue in Scala

Trying to create a MinMaxPriorityQueue using guava's library I am having the following issue:

This works:

val q: MinMaxPriorityQueue[String] = MinMaxPriorityQueue.
      maximumSize(10).
      create()
q.add("1.0")

But this not:

val q: MinMaxPriorityQueue[Double] = MinMaxPriorityQueue.
      maximumSize(10).
      create()
q.add(1.0)

As far as I've found while searching [1], this may be because Java treats scala's Double as a double in Java, and it not support Comparable. So I've tried this:

val q: MinMaxPriorityQueue[Comparable[Double]] = MinMaxPriorityQueue.
      maximumSize(10).
      create()
q.add(1.0)

This works, but when I try to add another element:

a.add(2.3)

it fails:

java.lang.ClassCastException: scala.runtime.RichDouble cannot be cast to java.lang.Double

It seems to be related to the issue mentioned in [1].

I've also tried to write a Type View:

class Test[A <% Comparable[A]](val q: MinMaxPriorityQueue[A])
val a = new Test[Double](MinMaxPriorityQueue.create())

Which gives the same error.

Is there a workaround to fix this?

Upvotes: 1

Views: 56

Answers (1)

Dima
Dima

Reputation: 40500

Use java.lang.Double perhaps?

 val q: MinMaxPriorityQueue[java.lang.Double] = ...

Should work.

Upvotes: 1

Related Questions