incompatible type in overriding in third-party classes

Viewed 189

i want define a hibernate PersistentSet for scala mutable Set, but i have this compile error :-(

class MySet extends org.hibernate.collection.internal.AbstractPersistentCollection with collection.mutable.Set[Any] {
  ...
}

error:

incompatible type in overriding
def empty(): Boolean (defined in class AbstractPersistentCollection);
 found   : scala.collection.mutable.Set[Any]
 required: (): Boolean
  override def empty: mutable.Set[Any] = ???

if there's a good way to fix it.

1 Answers

The only thing that would work in theory is defining def empty() in MySet with return type mutable.Set[Any] with Boolean. That should compile but it is impossible to find a reasonable implementation.

Alternatively you could define conversions between mutable.Set[Any] and AbstractPersistentCollection instead of using inheritance. You could do something like this:

class MySet private (private val set: mutable.Set[Any]) extends AbstractPersistentCollection {
  override def empty(): Boolean = set.isEmpty

  ...
}

object MySet {
  def empty(): MySet = new MySet(mutable.Set.empty[Any])
  def convert(set: mutable.Set[Any]): MySet = new MySet(set)
  def convert(set: MySet): mutable.Set[Any] = set.set
}
Related