Class 线性化在 scala 2.13 中不起作用
Class Linearization not working in scala 2.13
我正在尝试将 scala 2.12 升级到 scala 2.13.5
Class 线性化对我来说工作不正常,IntelliJ 和 Scala 编译器会抛出错误,但理想情况下它应该工作。下面是问题。
trait A[E] {
def getObject: E = {
// some implementation
}
}
abstract class B[T](e: Object) {
def getObject: T = {
// some implemntation
}
}
class C[T] extends B[T](null) with A[String] {
def myMethod(): Unit = {
println(this.getObject.someMethodWhichIsNotInStringClassButAvailableInTClass)
}
}
同样在上面的示例程序中,this.getObject
来自 A
,我希望它来自 B
。看来我的理解是错误的。但是需要详细了解这个classlinearization问题
由于上述问题,我的代码无法编译,因为所需的方法在 String
class.
中不可用
同样的代码也可以用 scala 2.12 编译,但不能用 scala 2.13.5 编译。
另一个参考 -
这根本不应该编译......而且它确实不适合我 (2.13.1):
error: class C inherits conflicting members:
def getObject: T (defined in trait B) and
def getObject: String (defined in trait A)
(note: this can be resolved by declaring an `override` in class C.)
您不能继承具有相同名称的不同方法。
此外,如果您的继承确实有效,线性化仍然会从左到右起作用:
trait A { def foo: Sting }
trait B extends A { def foo = "b" }
trait C extends A { def foo = "c" }
class D extends B with C
println(new D().foo)
这会打印“c”。
我正在尝试将 scala 2.12 升级到 scala 2.13.5
Class 线性化对我来说工作不正常,IntelliJ 和 Scala 编译器会抛出错误,但理想情况下它应该工作。下面是问题。
trait A[E] {
def getObject: E = {
// some implementation
}
}
abstract class B[T](e: Object) {
def getObject: T = {
// some implemntation
}
}
class C[T] extends B[T](null) with A[String] {
def myMethod(): Unit = {
println(this.getObject.someMethodWhichIsNotInStringClassButAvailableInTClass)
}
}
同样在上面的示例程序中,this.getObject
来自 A
,我希望它来自 B
。看来我的理解是错误的。但是需要详细了解这个classlinearization问题
由于上述问题,我的代码无法编译,因为所需的方法在 String
class.
同样的代码也可以用 scala 2.12 编译,但不能用 scala 2.13.5 编译。
另一个参考 -
这根本不应该编译......而且它确实不适合我 (2.13.1):
error: class C inherits conflicting members:
def getObject: T (defined in trait B) and
def getObject: String (defined in trait A)
(note: this can be resolved by declaring an `override` in class C.)
您不能继承具有相同名称的不同方法。
此外,如果您的继承确实有效,线性化仍然会从左到右起作用:
trait A { def foo: Sting }
trait B extends A { def foo = "b" }
trait C extends A { def foo = "c" }
class D extends B with C
println(new D().foo)
这会打印“c”。