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
@@ -84,39 +84,36 @@ So `circle.circumference` translates to `CircleOps.circumference(circle)`, provi
84
84
85
85
### Given Instances Defining Only Extension Methods
86
86
87
-
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
88
-
`extension`. E.g.,
87
+
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.
103
-
104
-
Note: `extension` is a soft keyword, it can be used elsewhere as a normal identifier.
101
+
If an extensions is anonymous (as in the second clause), its name is synthesized from the name of the first defined extension method.
105
102
106
103
### Given Extensions with Collective Parameters
107
104
108
105
If a given extension defines several extension methods one can pull out the left parameter section
109
106
as well as any type parameters of these extension methods into the given instance itself.
110
107
For instance, here is a given instance with two extension methods.
111
108
```scala
112
-
givenlistOps:extension {
109
+
givenlistOps: {
113
110
def (xs: List[T]) second[T]:T= xs.tail.head
114
111
def (xs: List[T]) third[T]:T= xs.tail.tail.head
115
112
}
116
113
```
117
114
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.
118
115
```scala
119
-
givenlistOps:extension[T](xs: List[T]) {
116
+
givenlistOps: [T](xs: List[T]) {
120
117
defsecond:T= xs.tail.head
121
118
defthird:T= xs.tail.tail.head
122
119
}
@@ -176,6 +173,6 @@ Here are the required syntax extensions compared to the
176
173
DefSig ::= ...
177
174
| ‘(’ DefParam ‘)’ [nl] id [DefTypeParamClause] DefParamClauses
0 commit comments