可能重复:
Why does one select Scala type members with a hash instead of a dot?'#'在类型参数中的含义是什么?
我有时会看到这样一种类型的参数:
class Test[K] {
type T = Foo[K#Bar]
}
可有人请解释一下什么是 '#' 这种类型的参数,意味着什么?是否对K
有某种限制?
可能重复:
Why does one select Scala type members with a hash instead of a dot?'#'在类型参数中的含义是什么?
我有时会看到这样一种类型的参数:
class Test[K] {
type T = Foo[K#Bar]
}
可有人请解释一下什么是 '#' 这种类型的参数,意味着什么?是否对K
有某种限制?
不,#是一个类型投影。在你的情况下,但不起作用,因为K没有定义任何BAR类型。
trait A {
type T
def apply():T
}
trait MyClass[X<:A] {
type SomeType = X#T
def applySeq():Traversable[SomeType]
}
class AImpl extends A {
type T=Int
def apply():Int = 10
}
class MyClassImpl extends MyClass[AImpl] {
def applySeq(): Traversable[SomeType] = List(10)
}
基本上可以让你在MyClass里面使用A的类型T.
事实上,还有如下编译:
class MyClassImpl extends MyClass[AImpl] {def applySeq(): Traversable[Int] = List(10)}
“#”用于投影出封闭在另一类型的类型。问题"Why does one select Scala type members with a hash instead of a dot"见this answer?
http://stackoverflow.com/questions/2183954/referring-to-the-type-of-an-inner-class-in-scala – 2012-07-11 07:45:35