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
+23-18Lines changed: 23 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ circle.circumference
22
22
### Translation of Extension Methods
23
23
24
24
Extension methods are methods that have a parameter clause in front of the defined identifier.
25
-
An extension method named `f` translates to method named `extension_f` that takes the leading parameter section as its first argument list.
25
+
An extension method named `f` translates to the method named `extension_f` that takes the leading parameter section as its first argument list.
26
26
So, the definition of `circumference` above translates to the following method, and can also be invoked as such:
27
27
28
28
```scala
@@ -76,7 +76,8 @@ extension [T: Numeric](x: T)
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
-
When calling a generic extension method, any explicitly given type arguments follow the method name. So the `second` method could be instantiated as follows.
79
+
When calling a generic extension method, any explicitly given type arguments follow the method name.
80
+
So the `second` method could be instantiated as follows:
**Note**: Type parameters have to be given after the `extension` keyword;
95
-
they cannot be given after the `def`. This restriction might be lifted in the future once we support multiple type parameter clauses in a method. By contrast, there can be using clauses in front as well as after the `def`.
95
+
**Note**: Type parameters have to be given after the `extension` keyword; they cannot be given after the `def`.
96
+
This restriction might be lifted in the future once we support multiple type parameter clauses in a method.
97
+
By contrast, using clauses can be defined for the `extension` as well as per `def`.
96
98
97
99
### Collective Extensions
98
100
99
101
Sometimes, one wants to define several extension methods that share the same
100
102
left-hand parameter type. In this case one can "pull out" the common parameters into
101
103
a single extension and enclose all methods in braces or an indented region following a '`:`'.
The same can be written with braces as follows (note that indented regions can still be used inside braces):
117
+
```scala
118
+
extension (ss: Seq[String]) {
119
+
120
+
deflongestStrings:Seq[String] =
121
+
valmaxLength= ss.map(_.length).max
122
+
ss.filter(_.length == maxLength)
123
+
124
+
deflongestString:String= longestStrings.head
125
+
}
126
+
```
127
+
114
128
Note the right-hand side of `longestString`: it calls `longestStrings` directly, implicitly
115
129
assuming the common extended value `ss` as receiver.
116
130
117
131
Collective extensions like these are a shorthand for individual extensions
118
-
where each method is defined separately. For instance, the first extension above expands to
132
+
where each method is defined separately. For instance, the first extension above expands to:
119
133
120
134
```scala
121
135
extension (ss: Seq[String])
@@ -127,16 +141,6 @@ extension (ss: Seq[String])
127
141
deflongestString:String= ss.longestStrings.head
128
142
```
129
143
130
-
Collective extensions also can take type parameters and have using clauses. Example
131
-
132
-
```scala
133
-
extension [T](xs: List[T])(usingOrdering[T]):
134
-
defsmallest(n: Int):List[T] = xs.sorted.take(n)
135
-
defsmallestIndices(n: Int):List[Int] =
136
-
vallimit= smallest(n).max
137
-
xs.zipWithIndex.collect { case (x, i) if x <= limit => i }
138
-
```
139
-
140
144
### Translation of Calls to Extension Methods
141
145
142
146
To convert a reference to an extension method, the compiler has to know about the extension
@@ -211,7 +215,8 @@ List(1, 2) < List(3)
211
215
212
216
The precise rules for resolving a selection to an extension method are as follows.
213
217
214
-
Assume a selection `e.m[Ts]` where `m` is not a member of `e`, where the type arguments `[Ts]` are optional, and where `T` is the expected type. The following two rewritings are tried in order:
218
+
Assume a selection `e.m[Ts]` where `m` is not a member of `e`, where the type arguments `[Ts]` are optional, and where `T` is the expected type.
219
+
The following two rewritings are tried in order:
215
220
216
221
1. The selection is rewritten to `extension_m[Ts](e)`.
217
222
2. If the first rewriting does not typecheck with expected type `T`,
0 commit comments