File tree 2 files changed +39
-0
lines changed
docs/docs/reference/other-new-features
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,36 @@ The correct way to write `E` is to extend both `Greeting` and
52
52
class E extends Greeting (" Bob" ), FormalGreeting
53
53
```
54
54
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
+
55
85
## Reference
56
86
57
87
For more information, see [ Scala SIP 25] ( http://docs.scala-lang.org/sips/pending/trait-parameters.html ) .
Original file line number Diff line number Diff line change @@ -16,4 +16,13 @@ class E extends Greeting("Bob") with FormalGreeting
16
16
17
17
// class D2 extends C with Greeting("Bill") // error
18
18
19
+ case class ImpliedName (name : String ):
20
+ override def toString = name
19
21
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
You can’t perform that action at this time.
0 commit comments