diff --git a/docs/docs/reference/other-new-features/trait-parameters.md b/docs/docs/reference/other-new-features/trait-parameters.md index 1655e338b32c..f930c10e2c5d 100644 --- a/docs/docs/reference/other-new-features/trait-parameters.md +++ b/docs/docs/reference/other-new-features/trait-parameters.md @@ -26,12 +26,14 @@ class D extends C, Greeting("Bill") // error: parameter passed twice Should this print "Bob" or "Bill"? In fact this program is illegal, because it violates the second rule of the following for trait parameters: - 1. If a class `C` extends a parameterized trait `T`, and its superclass does not, `C` _must_ pass arguments to `T`. + 1. If a class `C` directly extends a parameterized trait `T`, and its superclass does not, `C` _must_ pass arguments to `T`. - 2. If a class `C` extends a parameterized trait `T`, and its superclass does as well, `C` _must not_ pass arguments to `T`. + 2. If a class `C` directly or indirectly extends a parameterized trait `T`, and its superclass does as well, `C` _must not_ pass arguments to `T`. 3. Traits must never pass arguments to parent traits. + 4. If a class `C` extends a parameterized trait `T` only indirectly, and its superclass does not extend `T`, then all parameters of `T` must be defined via overrides. + Here's a trait extending the parameterized trait `Greeting`. ```scala @@ -51,6 +53,13 @@ The correct way to write `E` is to extend both `Greeting` and ```scala class E extends Greeting("Bob"), FormalGreeting ``` +Alternatively, a class could also define the `name` parameter of `Greeting` using +an override, using rule (4) above: + +```scala +class E2 extends FormalGreeting: + override val name: String = "Bob" +``` ## Reference diff --git a/tests/pos/i11214.scala b/tests/pos/i11214.scala new file mode 100644 index 000000000000..154f203b7e20 --- /dev/null +++ b/tests/pos/i11214.scala @@ -0,0 +1,5 @@ +trait Pet(val name: String) +trait FeatheredPet extends Pet + +class Bird(override val name: String) extends FeatheredPet: + override def toString = s"bird name: $name" \ No newline at end of file