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
+11-8Lines changed: 11 additions & 8 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 '`:`'.
102
-
Example:
104
+
Following an example using an indented region:
103
105
104
106
```scala
105
107
extension (ss: Seq[String]):
@@ -115,7 +117,7 @@ Note the right-hand side of `longestString`: it calls `longestStrings` directly,
115
117
assuming the common extended value `ss` as receiver.
116
118
117
119
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
120
+
where each method is defined separately. For instance, the first extension above expands to:
119
121
120
122
```scala
121
123
extension (ss: Seq[String])
@@ -211,7 +213,8 @@ List(1, 2) < List(3)
211
213
212
214
The precise rules for resolving a selection to an extension method are as follows.
213
215
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:
216
+
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.
217
+
The following two rewritings are tried in order:
215
218
216
219
1. The selection is rewritten to `extension_m[Ts](e)`.
217
220
2. If the first rewriting does not typecheck with expected type `T`,
0 commit comments