我想寫一個同質的元組類型,有點平行內置的元組類型在斯卡拉。斯卡拉高收斂類型的問題與新集合類
我有以下幾點:
trait HomogeneousTupleFactory [H <: HomogeneousTuple[_, H]] {
def makeHTuple[T] (values: Iterator[T]): HomogeneousTuple[T, H]
}
trait HomogeneousTuple [+T, H <: HomogeneousTuple[_, H]] extends Product {
def getFactory: HomogeneousTupleFactory[H]
def map [U] (fcn: T => U): HomogeneousTuple[U, H] = {
getFactory.makeHTuple(
this.productIterator.map(t => fcn(t.asInstanceOf[T]))
)
}
}
object HTuple2Factory extends HomogeneousTupleFactory[HTuple2[_]] {
def makeHTuple[T] (values: Iterator[T]): HTuple2[T] = {
new HTuple2(values.next, values.next)
}
}
class HTuple2[+T] (t1: T, t2: T) extends Tuple2(t1, t2)
with HomogeneousTuple[T, HTuple2[_]] {
def getFactory = HTuple2Factory
}
我想要得到它,這樣HTuple2.map[U]
返回HTuple2[U]
而不是HomogeneousTuple[U, HTuple2]
(這是合法的,正確的,但不太方便),但我可以不能讓它工作。
任何人有任何線索如何做到這一點?有沒有比我所做的更好的方式?
作爲一個側面說明,你可能想從無形退房大中 - HTTPS:/ /github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/sized.scala –
可能已經發誓我試過了,但我必須錯過或在某處添加了一個額外的[_]。非常感謝你。 –