Skip to content

Allow to define trait parameters via overrides #11338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions docs/docs/reference/other-new-features/trait-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/pos/i11214.scala
Original file line number Diff line number Diff line change
@@ -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"