Skip to content

Commit 5cd8439

Browse files
committed
Change docs as suggested in SIP meeting
1 parent 800251d commit 5cd8439

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ only be used for its extension methods.
166166
### Collective Extensions
167167

168168
Sometimes, one wants to define several extension methods that share the same
169-
left-hand parameter type. In this case one can "pull out" the common parameters
170-
into the extension instance itself. Examples:
169+
left-hand parameter type. In this case one can "pull out" the common parameters into the extension instance itself. Examples:
171170
```scala
172171
extension stringOps on (ss: Seq[String]) {
173172
def longestStrings: Seq[String] = {
@@ -186,6 +185,11 @@ extension on [T](xs: List[T])(using Ordering[T]) {
186185
def largest(n: Int) = xs.sorted.takeRight(n)
187186
}
188187
```
188+
**Note**: If a collective extension defines type parameters in its prefix
189+
(as the `listOps` extension above does), the extension methods themselves are not
190+
allowed to have additional type parameters. This restriction might be lifted in the
191+
future once we support multiple type parameter clauses in a method.
192+
189193
Collective extensions like these are a shorthand for extension instances where
190194
the parameters following the `on` are repeated for each implemented method.
191195
For instance, the collective extensions above expand to the following extension instances:
@@ -217,6 +221,18 @@ parameter `ss`. The usage is made explicit when translating the method:
217221
def (ss: Seq[String]).longestString: String =
218222
ss.longestStrings.head
219223
```
224+
By contrast, the meaning of `this` in a collective extension is as usual
225+
a reference to the enclosing object (i.e. the one implementing the extension methods).
226+
It's not a reference to the shared parameter. So this means that the following
227+
implementation of `longestString` would be illegal:
228+
```scala
229+
def longestString: String = this.longestStrings.head // error: missing parameter
230+
```
231+
But the following version would again be correct, since it calls the `longestString`
232+
method as a regular non-extension method, passing the prefix parameter `ss` as a regular parameter:
233+
```scala
234+
def longestString: String = this.longestStrings(ss).head
235+
```
220236

221237
### Syntax
222238

docs/docs/reference/contextual/given-imports.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ The following modifications avoid this hurdle to migration.
100100

101101
These rules mean that library users can use `given _` selectors to access old-style implicits in Scala 3.0,
102102
and will be gently nudged and then forced to do so in later versions. Libraries can then switch to
103-
representation clauses once their user base has migrated.
103+
given instances once their user base has migrated.
104104

105105
### Syntax
106106

docs/docs/reference/contextual/givens.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ given [T](using Ord[T]) as Ord[List[T]] { ... }
4848
If the name of a given is missing, the compiler will synthesize a name from
4949
the implemented type(s).
5050

51+
**Note** The name synthesized by the compiler is chosen to be readable and reasonably concise. For instance, the two instances above would get the names:
52+
```scala
53+
given_Ord_Int
54+
given_Ord_List_T
55+
```
56+
The precise rules for synthesizing names are found in [./relationship-implicit.html]. These rules do not guarantee absence of name conflicts between
57+
given instances of types that are "too similar". To avoid conflicts one can
58+
use named instances.
59+
60+
**Note** To ensure robust binary compatibility, publicly available libraries should prefer named instances.
61+
5162
## Alias Givens
5263

5364
An alias can be used to define a given instance that is equal to some expression. E.g.:

tests/new/test.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
trait T {
2-
object O
3-
}
1+
inline def foo = 1

0 commit comments

Comments
 (0)