Skip to content

Commit 4bbcdea

Browse files
committed
Update reference for ommiting trait parameters
1 parent 232180f commit 4bbcdea

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

docs/_docs/reference/other-new-features/trait-parameters.md

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,19 @@ The correct way to write `E` is to extend both `Greeting` and
5353
class E extends Greeting("Bob"), FormalGreeting
5454
```
5555

56-
## Traits With Context Parameters
56+
## Default values and implicit parameters
5757

58-
This "explicit extension required" rule is relaxed if the missing trait contains only
59-
[context parameters](../contextual/using-clauses.md). In that case the trait reference is
60-
implicitly inserted as an additional parent with inferred arguments. For instance,
61-
here's a variant of greetings where the addressee is a context parameter of type
62-
`ImpliedName`:
58+
If the trait `T` is parametrised such that `new T{}` would be a legal annonymous class creation, the "explicit extension required" rule does not apply, the parameters filled in as for the annonymous class:
59+
* Parameters with default values are set to their default value.
60+
* [Context parameters](../contextual/using-clauses.md) are set to the value present in the implicit scope. Notably from class parameters or other traits.
61+
62+
```scala
63+
trait withLastName(val lastName: String = "")
64+
65+
case class Person extends withLastName
66+
// identical to
67+
case class Person extends withLastName("")
68+
```
6369

6470
```scala
6571
case class ImpliedName(name: String):
@@ -83,6 +89,33 @@ class F(using iname: ImpliedName) extends
8389
```
8490
Note the inserted reference to the super trait `ImpliedGreeting`, which was not mentioned explicitly.
8591

92+
In the above context, the following is also valid:
93+
```scala
94+
given iname: ImpliedName
95+
class F2() extends ImpliedFormalGreeting
96+
```
97+
And expands to:
98+
```scala
99+
class F2() extends
100+
Object,
101+
ImpliedGreeting(using iname),
102+
ImpliedFormalGreeting
103+
```
104+
105+
Forms of ommission can be combined:
106+
```scala
107+
trait SuperTrait(using Char)(val name: String = "")(using Int)
108+
109+
given c: Char = 'X'
110+
class SuperClass(using i: Int) extends SuperTrait
111+
```
112+
The last line expands to:
113+
114+
```scala
115+
class SuperClass(using i: Int) extends SuperTrait(using c)("")(using i)
116+
```
117+
86118
## Reference
87119

88120
For more information, see [Scala SIP 25](http://docs.scala-lang.org/sips/pending/trait-parameters.html).
121+
TODO: Find which PR changed the behaviour, as this was changed between 3.2.2 and 3.2.1

0 commit comments

Comments
 (0)