문제

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?

도움이 되었습니까?

해결책

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top