Skip to content

Commit e5df10a

Browse files
committed
Add explanations to doc page
1 parent c4b9060 commit e5df10a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ The correct way to write `E` is to extend both `Greeting` and
5252
class E extends Greeting("Bob"), FormalGreeting
5353
```
5454

55+
### Traits With Context Parameters
56+
57+
This "explicit extension required" rule is relaxed if the missing trait contains only
58+
[context parameters](../contextual/using-clauses). In that case the trait reference is
59+
implicitly inserted as an additional parent with inferred arguments. For instance,
60+
here's a variant of greetings where the addressee is a context parameter of type
61+
`ImpliedName`:
62+
63+
```scala
64+
case class ImpliedName(name: String):
65+
override def toString = name
66+
67+
trait ImpliedGreeting(using val iname: ImpliedName):
68+
def msg = s"How are you, $iname"
69+
70+
trait ImpliedFormalGreeting extends ImpliedGreeting:
71+
override def msg = s"How do you do, $iname"
72+
73+
class F(using iname: ImpliedName) extends ImpliedFormalGreeting
74+
```
75+
76+
The definition of `F` in the last line is implicitly expanded to
77+
```scala
78+
class F(using iname: ImpliedName) extends
79+
Object,
80+
ImpliedGreeting(using iname),
81+
ImpliedFormalGreeting(using iname)
82+
```
83+
Note the inserted reference to the super trait `ImpliedGreeting`, which was not mentioned explicitly.
84+
5585
## Reference
5686

5787
For more information, see [Scala SIP 25](http://docs.scala-lang.org/sips/pending/trait-parameters.html).

tests/pos/reference/trait-parameters.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@ class E extends Greeting("Bob") with FormalGreeting
1616

1717
// class D2 extends C with Greeting("Bill") // error
1818

19+
case class ImpliedName(name: String):
20+
override def toString = name
1921

22+
trait ImpliedGreeting(using val iname: ImpliedName):
23+
def msg = s"How are you, $iname"
24+
25+
trait ImpliedFormalGreeting extends ImpliedGreeting:
26+
override def msg = s"How do you do, $iname"
27+
28+
class F(using iname: ImpliedName) extends ImpliedFormalGreeting

0 commit comments

Comments
 (0)