Pregunta

Given traits:

trait HasSize {
  def size() : Int
}

trait StorageTrait extends HasSize {
  def something() : Unit
}

trait YetAnotherStorageTrait extends HasSize {
  def anotherSomething() : Unit
}

I want to create class

class InMemoryStorage extends StorageTrait with YetAnotherStorageTrait {
   //...
}

now, method size of StorageTrait returns different thing than method YetAnotherStorageTrait (it's still a size, but of different collections).

What is the correct way to design such case in Scala?

¿Fue útil?

Solución

If I understand the question correctly, you have a class which inherits traits which respond differently to the size method? Then there is no way to resolve this ambiguity than to use composition instead of mixin.

class InMemoryStorage
  def mainStorage: StorageTrait
  def auxStorage: YetAnotherStorageTrait

  def mainSize = mainStorage.size
  def auxSize  = auxStorage.size
}

Is that what you are looking after?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top