Note that P
must be a parent type, not any supertype. I.e. if you have class D extends C with T1 with T2
, and there is a hello
method in class C
and traits T1
and T2
, just writing super.hello
wouldn't tell the compiler which hello
method you want. So you can write super[C].hello
, super[T1].hello
or super[T2].hello
.
How to understand `C.super[P]` in scala?
Question
I'm reading the book of "The depth of scala", and on chapter 6 "types", page 123:
The path C.super or C.super[P] where C refers to a class and P refers to a par- ent type of class C. Using the super keyword directly is shorthand for C.super. Use this path to disambiguate between identifiers defined on a class and a par- ent class.
I can't understand the C.super[P]
, why we can specify the type P
after super
?
I tried this scala code:
class P {
def hello = "hello from P"
}
class C extends P {
override def hello = "hello from C"
}
class D extends C {
override def hello = super[P].hello
}
val d = new D
d.hello
Which is unable to compile on this line:
override def hello = super[P].hello
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow