You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/extension-methods.md
+7-10Lines changed: 7 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,39 +80,36 @@ So `circle.circumference` translates to `CircleOps.circumference(circle)`, provi
80
80
81
81
### Given Instances Defining Only Extension Methods
82
82
83
-
Given instances that define extension methods can also be defined without a parent clause. In this case the `given` is followed by the special identifier
84
-
`extension`. E.g.,
83
+
Given instances that define extension methods can also be defined without a parent clause. E.g.,
If given extensions are anonymous (as in the second clause), their name is synthesized from the name of the first defined extension method.
99
-
100
-
Note: `extension` is a soft keyword, it can be used elsewhere as a normal identifier.
97
+
If an extensions is anonymous (as in the second clause), its name is synthesized from the name of the first defined extension method.
101
98
102
99
### Given Extensions with Collective Parameters
103
100
104
101
If a given extension defines several extension methods one can pull out the left parameter section
105
102
as well as any type parameters of these extension methods into the given instance itself.
106
103
For instance, here is a given instance with two extension methods.
107
104
```scala
108
-
givenlistOps:extension {
105
+
givenlistOps: {
109
106
def (xs: List[T]) second[T]:T= xs.tail.head
110
107
def (xs: List[T]) third[T]:T= xs.tail.tail.head
111
108
}
112
109
```
113
110
The repetition in the parameters can be avoided by moving the parameters in front of the opening brace. The following version is a shorthand for the code above.
114
111
```scala
115
-
givenlistOps:extension[T](xs: List[T]) {
112
+
givenlistOps: [T](xs: List[T]) {
116
113
defsecond:T= xs.tail.head
117
114
defthird:T= xs.tail.tail.head
118
115
}
@@ -172,6 +169,6 @@ Here are the required syntax extensions compared to the
172
169
DefSig ::= ...
173
170
| ‘(’ DefParam ‘)’ [nl] id [DefTypeParamClause] DefParamClauses
0 commit comments