Skip to content

Commit 7e359cd

Browse files
committed
doc(extension method): typos and improvements
1 parent 63b84cb commit 7e359cd

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

docs/docs/reference/contextual/extension-methods.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ circle.circumference
2222
### Translation of Extension Methods
2323

2424
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.
2626
So, the definition of `circumference` above translates to the following method, and can also be invoked as such:
2727

2828
```scala
@@ -76,7 +76,8 @@ extension [T: Numeric](x: T)
7676
```
7777

7878
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:
8081

8182
```scala
8283
List(1, 2, 3).second[Int]
@@ -91,15 +92,16 @@ extension [T](x: T)(using n: Numeric[T])
9192
def + (y: T): T = n.plus(x, y)
9293
```
9394

94-
**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`.
9698

9799
### Collective Extensions
98100

99101
Sometimes, one wants to define several extension methods that share the same
100102
left-hand parameter type. In this case one can "pull out" the common parameters into
101103
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:
103105

104106
```scala
105107
extension (ss: Seq[String]):
@@ -115,7 +117,7 @@ Note the right-hand side of `longestString`: it calls `longestStrings` directly,
115117
assuming the common extended value `ss` as receiver.
116118

117119
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:
119121

120122
```scala
121123
extension (ss: Seq[String])
@@ -211,7 +213,8 @@ List(1, 2) < List(3)
211213

212214
The precise rules for resolving a selection to an extension method are as follows.
213215

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:
215218

216219
1. The selection is rewritten to `extension_m[Ts](e)`.
217220
2. If the first rewriting does not typecheck with expected type `T`,
@@ -252,7 +255,7 @@ def extension_position(s: String)(ch: Char, n: Int): Int =
252255
1. To avoid confusion, names of normal methods are not allowed to start with `extension_`.
253256

254257
2. A named import such as `import a.m` of an extension method in `a` will make `m` only available as an extension method.
255-
To access it under `extension_m` that name as to be imported separately. Example:
258+
To access it under `extension_m` that name has to be imported separately. Example:
256259

257260
```scala
258261
object DoubleOps:

0 commit comments

Comments
 (0)