나는이 같은 trait
있습니다. 예를 들어 :스칼라 패턴 매칭의 조합 유형은
case class EntityA(id: Option[Long], name: String, created: Date) extends Identifiable
case class EntityB(id: Option[Long], price: Long, count: Int) extends Identifiable
내가 Seq[Identifiable]
가 있다고 가정하고 나는 각 사람에게 새로운 id
을 할당 할.
val xs: Seq[Identifiable] = ...
xs.map {
case x: EntityA => x.copy(id = Some(nextId))
case x: EntityB => x.copy(id = Some(nextId))
}
좋은 :
가장 간단한 방법은 것 같다! 하지만 문제가 있습니다. 하위 클래스가 많을수록 더 많은 (중복 된) 코드가 작성됩니다.
나는 연합 유형에서 도움을받을려고 :
xs.map {
case x: EntityA with EntityB => x.copy(id = Some(nextId))
}
또는
xs.map {
case x @ (_: EntityA | _: EntityB) => x.copy(id = Some(nextId))
}
하지만 난라는 오류가있어 :
Cannot resolve symbol copy
어떤 도움을 주시면 감사하겠습니다을. 감사합니다. .
또한 관련을 : http://stackoverflow.com/questions/40995639/scala-how-to-define-an-abstract 당신이 뭘 하려는지 꽤 유사하다이 답변을 참조하십시오 -copyable-superclass-for-any-case-class –