下記は動作する
LegalBuz.scala
package test
trait Foo {
def hello = "foo"
}
trait Bar {
def hello = "bar"
}
class Buz extends Foo with Bar{
override val hello = super[Foo].hello
def sayHello():Unit = {
println(hello)
}
}
object RunBuz extends App{
new Buz().sayHello()
}
FooとBarのdefをvalにしてみると動作しない
IllegalBuz.scala
package test
trait Foo {
val hello = "foo"
}
trait Bar {
val hello = "bar"
}
class Buz extends Foo with Bar{
override val hello = super[Foo].hello
def sayHello():Unit = {
println(hello)
}
}
object RunBuz extends App{
new Buz().sayHello()
}
error: super may be not be used on value hello
override val hello = super[Foo].hello
may be not be...?
下記も動く(結果はbuz)
YetAnotherLegalBuz.scala
package test
trait Foo {
val hello = "foo"
}
trait Bar {
val hello = "bar"
}
class Buz extends Foo with Bar{
override val hello = "buz"
def sayHello():Unit = {
println(hello)
}
}
object RunBuz extends App{
new Buz().sayHello()
}
valをリテラルで置き換えることはできるけど、どちらかのtraitで宣言されているものは参照できないのか?
と思っていたら
https://issues.scala-lang.org/browse/SI-1938
こういうのがあったので、やはりできなかったというお話。