Skip to content

Commit 3b8ae8d

Browse files
committed
doc(extension method): add example with braces
and remove note about type parameters for collective extensions. Since we have a unified syntax it should be clear that the same we explained for extension methods also apply to collective extensions as they are the same thing.
1 parent 7e359cd commit 3b8ae8d

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ extension (ss: Seq[String]):
113113
def longestString: String = longestStrings.head
114114
```
115115

116+
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+
def longestStrings: Seq[String] =
121+
val maxLength = ss.map(_.length).max
122+
ss.filter(_.length == maxLength)
123+
124+
def longestString: String = longestStrings.head
125+
}
126+
```
127+
116128
Note the right-hand side of `longestString`: it calls `longestStrings` directly, implicitly
117129
assuming the common extended value `ss` as receiver.
118130

@@ -129,16 +141,6 @@ extension (ss: Seq[String])
129141
def longestString: String = ss.longestStrings.head
130142
```
131143

132-
Collective extensions also can take type parameters and have using clauses. Example
133-
134-
```scala
135-
extension [T](xs: List[T])(using Ordering[T]):
136-
def smallest(n: Int): List[T] = xs.sorted.take(n)
137-
def smallestIndices(n: Int): List[Int] =
138-
val limit = smallest(n).max
139-
xs.zipWithIndex.collect { case (x, i) if x <= limit => i }
140-
```
141-
142144
### Translation of Calls to Extension Methods
143145

144146
To convert a reference to an extension method, the compiler has to know about the extension

0 commit comments

Comments
 (0)