나는, 나는이 같은 집계의 몇 가지있다이이 스칼라 코드가 유추하지 않는 이유는 무엇입니까?
trait Aggregate[T <: Aggregate[T, K], K] { self: T =>
def plus(another: T): T
def show: K
}
과 같은 몇 가지 코드와 통합 프레임 워크를 구축하고
case class Count(value: Long = 1) extends Aggregate[Count, Long] {
def plus(another: Count) = Count(value + another.value)
def show = value
}
내가 같이하는 집계를 정의하면
,case class By[T <: Aggregate[T, K], K, G](values: HashMap[G, T]) extends Aggregate[By[T, K, G], Map[G, K]] {
override def plus(another: By[T, K, G]): By[T, K, G] = By(values.merged(another.values){case ((k1,v1), (k2,v2)) => (k1, v1 plus v2)})
override def show: Map[G, K] = values.map{case (k,v) => k -> v.show}
}
object By {
def apply[T <: Aggregate[T,K], K, G](key:G, value:T):By[T, K, G] = By(HashMap(key -> value))
}
다음과 같이 쓸 수 없습니다.
4,대신 나는, 나는 그것을 달성하기 위해 깨끗한 방법이 그 유형을 지정 싫어 자사가 Long
부분을 파악하지 이후이
By[Count, Long, String]("key1", Count(100))
를 작성했다?
와우, 멋지게 작동합니다. –
값은 기술적으로 [복합 유형] (http://docs.scala-lang.org/tutorials/tour/compound-types)입니다. –
이상적으로 말하면,'T'는'Aggregate [T, K]'를 확장한다고 말했기 때문에 필요하지 않습니다. 내가 볼 수없는 통일 알고리즘의 기술적 한계가있을 수 있습니다. –