From faf8716f3d392d3597fa0c3ec359223a255d4b4b Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sun, 7 Feb 2021 11:42:38 +0100 Subject: [PATCH 1/2] Allow to define trait parameters via overrides This legalizes the behavior exhibited in #11214. Fixes #11214 --- .../other-new-features/trait-parameters.md | 13 +++++++++++-- tests/pos/i11214.scala | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 tests/pos/i11214.scala 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..0f34dbce4bbe --- /dev/null +++ b/tests/pos/i11214.scala @@ -0,0 +1,5 @@ +trait Pet(val name: String) +trait FeatheredPet extends Pet + +class Bird(name: String) extends FeatheredPet: + override def toString = s"bird name: $name" \ No newline at end of file From 0823a8f0a36c8adcb760bf3e293cb41ae14b36eb Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sun, 7 Feb 2021 18:46:33 +0100 Subject: [PATCH 2/2] Fix test --- tests/pos/i11214.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pos/i11214.scala b/tests/pos/i11214.scala index 0f34dbce4bbe..154f203b7e20 100644 --- a/tests/pos/i11214.scala +++ b/tests/pos/i11214.scala @@ -1,5 +1,5 @@ trait Pet(val name: String) trait FeatheredPet extends Pet -class Bird(name: String) extends FeatheredPet: +class Bird(override val name: String) extends FeatheredPet: override def toString = s"bird name: $name" \ No newline at end of file