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
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -65,30 +65,30 @@ the two swaps cancel each other out).
65
65
66
66
### Generic Extensions
67
67
68
-
It is also possible to extend generic types by adding type parameters to an extension. For instance:
68
+
It is also possible to extend generic types by adding type parameters to an extension. For instance:
69
69
70
-
```scala
71
-
extension [T](xs: List[T])
72
-
defsecond= xs.tail.head
70
+
```scala
71
+
extension [T](xs: List[T])
72
+
defsecond= xs.tail.head
73
73
74
-
extension [T:Numeric](x: T)
75
-
def+ (y: T):T= summon[Numeric[T]].plus(x, y)
74
+
extension [T:Numeric](x: T)
75
+
def+ (y: T):T= summon[Numeric[T]].plus(x, y)
76
76
```
77
77
78
78
If an extension method has type parameters, they come immediately after `extension` and are followed by the extended parameter.
79
79
When calling a generic extension method, any explicitly given type arguments follow the method name. So the `second` method could be instantiated as follows.
80
80
81
81
```scala
82
-
List(1, 2, 3).second[Int]
82
+
List(1, 2, 3).second[Int]
83
83
```
84
84
85
85
Of course, the type argument here would usually be left out since it can be inferred.
86
86
87
87
Extensions can also take using clauses. For instance, the `+` extension above could equivalently be written with a using clause:
88
88
89
89
```scala
90
-
extension [T](x: T)(usingn: Numeric[T])
91
-
def- (y: T):T= n.minus(x, y)
90
+
extension [T](x: T)(usingn: Numeric[T])
91
+
def+ (y: T):T= n.plus(x, y)
92
92
```
93
93
94
94
**Note**: Type parameters have to be given after the `extension` keyword;
0 commit comments