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
Syntax change for type parameters of extension methods
The new syntax has the type parameters come first. I.e.
```
def [T](xs: List[T]) append (ys: List[T]): List[T] = ...
```
instead of
```
def (xs: List[T]) append [T] (ys: List[T]): List[T] = ...
```
Application is unchanged, so it is still
```
xs.append[String](ys)
```
An argument for the old syntax is that it aligns definition and call syntax.
On the other hand, the new syntax maintains the general rule that parameter introductions
always com before parameter uses. The decisive argument to switch is to be consistent
with the new collective parameter syntax, where `append` would be written like this:
```
given [T](xs: List[T])
def append (ys: List[T]): List[T] = ...
```
To avoid misalignment of type parameters between definition and call syntax, we considered
disallowing explicit type parameters for extension methods altogether, and to require
that the method is called as a normal method instead. But that would not work for anonymous
givens as in the last exaple above.
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/extension-methods.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,7 @@ given stringOps: {
94
94
}
95
95
96
96
given {
97
-
def (xs: List[T]) second[T]= xs.tail.head
97
+
def[T](xs: List[T]) second = xs.tail.head
98
98
}
99
99
```
100
100
If such given instances are anonymous (as in the second clause), their name is synthesized from the name of the first defined extension method.
@@ -106,8 +106,8 @@ as well as any type parameters of these extension methods into the given instanc
106
106
For instance, here is a given instance with two extension methods.
107
107
```scala
108
108
givenlistOps: {
109
-
def (xs: List[T]) second[T]:T= xs.tail.head
110
-
def (xs: List[T]) third[T]:T= xs.tail.tail.head
109
+
def[T](xs: List[T]) second: T= xs.tail.head
110
+
def[T](xs: List[T]) third: T= xs.tail.tail.head
111
111
}
112
112
```
113
113
The repetition in the parameters can be avoided by hoisting the parameters up into the given instance itself. The following version is a shorthand for the code above.
@@ -151,13 +151,13 @@ to the implementation of right binding operators as normal methods.
151
151
The `StringSeqOps` examples extended a specific instance of a generic type. It is also possible to extend a generic type by adding type parameters to an extension method. Examples:
152
152
153
153
```scala
154
-
def (xs: List[T]) second [T]=
154
+
def[T](xs: List[T]) second =
155
155
xs.tail.head
156
156
157
-
def (xs: List[List[T]]) flattened [T]=
157
+
def[T](xs: List[List[T]]) flattened =
158
158
xs.foldLeft[List[T]](Nil)(_ ++ _)
159
159
160
-
def (x: T) +[T:Numeric](y: T):T=
160
+
def[T:Numeric](x: T) + (y: T):T=
161
161
summon[Numeric[T]].plus(x, y)
162
162
```
163
163
@@ -170,7 +170,7 @@ The required syntax extension just adds one clause for extension methods relativ
170
170
to the [current syntax](../../internals/syntax.md).
171
171
```
172
172
DefSig ::= ...
173
-
| ‘(’ DefParam ‘)’ [nl] id [DefTypeParamClause] DefParamClauses
0 commit comments