考虑下面的代码:为什么Scala在分配给抽象类型时会丢失实际的类型?
abstract class Foobar {
type Parent <: Foobar
def parent: Option[Parent]
}
class Foo extends Foobar {
type Parent = Nothing
val parent = None
// ***
// Taking the explicit declaration of the "bar" method's return type
// "Bar" away convinces Scala to compile the code. In other words:
//
// def bar() = new Bar
// ***
def bar(): Bar = new Bar {
type Parent = Foo
val parent = Some(Foo.this)
}
}
abstract class Bar extends Foobar
(new Foo).bar().parent.get.bar()
我的问题:
你能解释一下为什么会这样?请参阅上面的内嵌评论。
你有解决方案吗?
我碰到斯卡拉-SDK的斯卡拉工作表以下错误消息版本3.0.2-vfinal-20131028-1923类型安全:
Multiple markers at this line
- value bar is not a member of scrap.Bar#Parent
- value bar is not a member of scrap.Bar#Parent
确定,离开了 “栏”方法的返回类型的声明让我从斯卡拉工作表以下信息消息:
> res0: scrap.Bar{type Parent = scrap.Foo; val parent: Some[scrap.Foo]} = scra
//| [email protected]
有没有办法给这个类型一个体面的名字,非常“Foobar的#父”?
请张贴的编译器错误。我知道这看起来很迂腐,但未来版本的scalac可能会有不同的表现。 –