所以這是this Java question Scala的一個相當直接端口在斯卡拉避免重複泛型參數
我們有一堆採取通用的參數特性如下:
trait Ident { }
trait Container[I <: Ident] {
def foo(id: I): String
}
trait Entity[C <: Container[I], I <: Ident] {
def container: C
def foo(id: I) = container.foo(id)
}
這工作,但它是一個小因爲在定義實體的子類時,我們必須提供Ident的類型和Container的類型。而事實上,就在容器的類型將是自身足夠的類型信息:
class MyIdent extends Ident { }
class MyContainer extends Container[MyIdent] { }
class MyEntity extends Entity[MyContainer,MyIdent] { }
// ^^^^^^^ shouldn't really be necessary
使用一個存在的類型避免了需要實體採取兩個參數......當然你不能引用它稍後的。
trait Entity[C <: Container[I] forSome { type I <: Ident }] {
def container: C
def foo(id: I) = container.foo(id)
// ^^^ complains it has no idea what 'I' is here
}
同樣將使用成員類型也不起作用的東西...
trait Ident { }
trait Container {
type I <: Ident
def foo(id: I): String
}
trait Entity {
type C <: Container
def container: C
def foo(id: C#I) = container.foo(id)
// ^^ type mismatch
}
因此,沒有人知道,如果有一個優雅的解決方案,以在斯卡拉這個問題呢?
我不認爲答案會與Java版本大不相同。沒有一種真正的方法可以省略類型參數,而不會丟失隨之而來的類型信息。 –
是否可以將'container'設爲'val'? – sepp2k
@MichaelZajac所以在「MyEntity」的定義中,提供給Entity的第二個參數是多餘的:除了「MyIdent」之外,根本沒有其他可能的類型可用,實際上其他任何類型都會導致編譯錯誤。當然,是否可以避免Scala中的冗餘是另一個問題:-) –