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
@@ -78,13 +78,13 @@ and where `T` is the expected type. The following two rewritings are tried in or
78
78
So `circle.circumference` translates to `CircleOps.circumference(circle)`, provided
79
79
`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`).
80
80
81
-
### Given Instances for Extension Methods
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
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
If given extensions are anonymous (as in the second clause), their name is synthesized from the name of the first defined extension method.
99
99
100
-
Note: `extension` is a soft keyword, it can be used elsewhere
101
-
as a normal identifier.
100
+
Note: `extension` is a soft keyword, it can be used elsewhere as a normal identifier.
102
101
103
102
### Given Extensions with Collective Parameters
104
103
105
104
If a given extension defines several extension methods one can pull out the left parameter section
106
105
as well as any type parameters of these extension methods into the given instance itself.
107
106
For instance, here is a given instance with two extension methods.
108
107
```scala
109
-
givenextensionlistOps: {
108
+
givenlistOps: extension {
110
109
def (xs: List[T]) second[T]:T= xs.tail.head
111
110
def (xs: List[T]) third[T]:T= xs.tail.tail.head
112
111
}
113
112
```
114
113
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.
115
114
```scala
116
-
givenextensionlistOps: [T](xs: List[T]) {
115
+
givenlistOps:extension[T](xs: List[T]) {
117
116
defsecond:T= xs.tail.head
118
117
defthird:T= xs.tail.tail.head
119
118
}
@@ -167,11 +166,12 @@ As usual, type parameters of the extension method follow the defined method name
167
166
168
167
### Syntax
169
168
170
-
The required syntax extension just adds one clause for extension methods relative
171
-
to the [current syntax](../../internals/syntax.md).
169
+
Here are the required syntax extensions compared to the
170
+
[current syntax](../../internals/syntax.md).
172
171
```
172
+
DefSig ::= ...
173
+
| ‘(’ DefParam ‘)’ [nl] id [DefTypeParamClause] DefParamClauses
0 commit comments