2
我想創建一個Scala Id類型類型,例如,我可以聲明類型爲Foo的Id需要Long值,例如,我如何讓scala編譯器從另一個類推斷一種類型?
val fooId: Id[Foo] = Id(12L) // type-safe at compile time
val fooIdValue: Long = fooId.value // able to get the value back out
我試過各種方法,但我似乎無法強制約束。如果我宣佈
trait WithId[I] {
type Id = I
}
case class Id[A <: WithId[_]](value: A#Id) // A#Id => Any, not what I want!
class Foo extends WithId[Long] {
type Id = Long
}
這使得
val fooId: Id[Foo] = Id("foo") // should be illegal unless a Long
如果我改變WithId使用抽象類型
trait WithId {
type Id
}
case class Id[A <: WithId](value: A#Id)
class Foo extends WithId {
type Id = Long
}
然後
val fooId: Id[Foo] = Id(12L)
不能編譯,說
no type parameters for method apply: (value: A#Id)net.box.Id[A] in object Id exist so that it can be applied to arguments (Long) --- because --- argument expression's type is not compatible with formal parameter type; found : Long required: ?0A#Id
我該怎麼說和強制Id [Foo]需要一個長?
有趣。我沒有想到這種類型的「切換邊」會很重要,因此可以這麼說,如果它在左邊,它會被插入到右邊。謝謝! – 2012-04-03 20:53:13