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
Change from
```
given extension foo: { ... }
```
to
```
given foo: extension { ... }
```
The new syntax is more regular and pleasing to the eye. It avoids
the unusual `: { ... }` symbol combination.
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/extension-methods.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,13 +82,13 @@ and where `T` is the expected type. The following two rewritings are tried in or
82
82
So `circle.circumference` translates to `CircleOps.circumference(circle)`, provided
83
83
`circle` has type `Circle` and `CircleOps` is given (i.e. it is visible at the point of call or it is defined in the companion object of `Circle`).
84
84
85
-
### Given Instances for Extension Methods
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
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
If given extensions are anonymous (as in the second clause), their name is synthesized from the name of the first defined extension method.
103
103
104
-
Note: `extension` is a soft keyword, it can be used elsewhere
105
-
as a normal identifier.
104
+
Note: `extension` is a soft keyword, it can be used elsewhere as a normal identifier.
106
105
107
106
### Given Extensions with Collective Parameters
108
107
109
108
If a given extension defines several extension methods one can pull out the left parameter section
110
109
as well as any type parameters of these extension methods into the given instance itself.
111
110
For instance, here is a given instance with two extension methods.
112
111
```scala
113
-
givenextensionlistOps: {
112
+
givenlistOps: extension {
114
113
def (xs: List[T]) second[T]:T= xs.tail.head
115
114
def (xs: List[T]) third[T]:T= xs.tail.tail.head
116
115
}
117
116
```
118
117
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.
119
118
```scala
120
-
givenextensionlistOps: [T](xs: List[T]) {
119
+
givenlistOps:extension[T](xs: List[T]) {
121
120
defsecond:T= xs.tail.head
122
121
defthird:T= xs.tail.tail.head
123
122
}
@@ -171,11 +170,12 @@ As usual, type parameters of the extension method follow the defined method name
171
170
172
171
### Syntax
173
172
174
-
The required syntax extension just adds one clause for extension methods relative
175
-
to the [current syntax](../../internals/syntax.md).
173
+
Here are the required syntax extensions compared to the
174
+
[current syntax](../../internals/syntax.md).
176
175
```
176
+
DefSig ::= ...
177
+
| ‘(’ DefParam ‘)’ [nl] id [DefTypeParamClause] DefParamClauses
0 commit comments