scala - this 和 this.type 之间的关系

标签 scala

以下内容:

trait Foo {
    type T
    val foo: T
}

trait Bar extends Foo {
    type T = this.type
    val foo = this
}

给出编译器错误:

<console>:8: error: overriding value foo in trait Foo of type Bar.this.T;
 value foo has incompatible type
        val foo = this
            ^

但是,如果我将最后一行更改为:

val foo: this.type = this

编译没有错误。

为什么我必须在这里明确指定类型?我已经说过 foo 的类型应该是 T 并且 T 应该是 this.typethis 的类型不是 this.type 吗?

最佳答案

关于scala - this 和 this.type 之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6958668/

相关文章:

scala - 在 ScalaCheck 中从语法生成字符串

scala - 对于基本数据框创建示例,我应该如何在 Spark 中编写单元测试?

scala - 在 Lift 和 CRUDify 中使用枚举字段

scala - 关键字 'implicit' 放在 lambda 表达式参数前面是什么意思?

scala案例类私有(private)应用方法(repl bug?)

scala - Spark : Get top N by key

scala - 如何在Scala中制作 "n to m"受限制的int变体?

scala - 这个排列函数是如何工作的(Scala)?

scala 解析器组合器中的 Java 正则表达式

ScalaTest 运行时的 Scala Play Slick RejectedExecutionException