所以这是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中的冗余是另一个问题:-) –