Skip to content

Commit d87a89e

Browse files
authored
Merge pull request #6430 from dotty-staging/try-contextual-delegate
Tweaks to instance evaluation
2 parents 69fcf23 + 3e0edbd commit d87a89e

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

docs/docs/reference/contextual-implicit/relationship-implicits.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,23 @@ Implicit instances can be mapped to combinations of implicit objects, classes an
2828
class ListOrd[T](implicit ord: Ord[T]) extends Ord[List[T]] { ... }
2929
final implicit def ListOrd[T](implicit ord: Ord[T]): ListOrd[T] = new ListOrd[T]
3030
```
31-
3. Alias implicits map to implicit methods. If the implicit has neither type parameters nor a given clause, the result of creating an instance is cached in a variable. If in addition the right hand side is pure and cheap to compute, a simple `val` can be used instead. E.g.,
31+
3. Alias implicits map to implicit methods. If the implicit has neither type parameters nor a given clause, the result of creating an instance is cached in a variable. There are two cases that can be optimized:
32+
33+
- If the right hand side is a simple reference, we can
34+
use a forwarder to that reference without caching it.
35+
- If the right hand side is more complex, but still known to be pure, we can
36+
create a `val` that computes it ahead of time.
37+
38+
Examples:
39+
3240
```scala
3341
implicit global for ExecutionContext = new ForkJoinContext()
3442
implicit config for Config = default.config
43+
44+
def ctx: Context
45+
implicit for Context = ctx
3546
```
36-
map to
47+
would map to
3748
```scala
3849
private[this] var global$cache: ExecutionContext | Null = null
3950
final implicit def global: ExecutionContext = {
@@ -42,6 +53,8 @@ Implicit instances can be mapped to combinations of implicit objects, classes an
4253
}
4354

4455
final implicit val config: Config = default.config
56+
57+
final implicit def Context_repr = ctx
4558
```
4659

4760
### Anonymous Implicits

docs/docs/reference/contextual-repr/relationship-implicits.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,23 @@ Representative clauses can be mapped to combinations of implicit objects, classe
2828
class ListOrd[T](implicit ord: Ord[T]) extends Ord[List[T]] { ... }
2929
final implicit def ListOrd[T](implicit ord: Ord[T]): ListOrd[T] = new ListOrd[T]
3030
```
31-
3. Alias representatives map to implicit methods. If the representative has neither type parameters nor a given clause, the result of creating an instance is cached in a variable. If in addition the right hand side is pure and cheap to compute, a simple `val` can be used instead. E.g.,
31+
3. Alias representatives map to implicit methods. If the representative has neither type parameters nor a given clause, the result of creating an instance is cached in a variable. There are two cases that can be optimized:
32+
33+
- If the right hand side is a simple reference, we can simply
34+
use a forwarder to that reference without caching it.
35+
- If the right hand side is more complex, but still known to be pure, we can
36+
create a `val` that computes it ahead of time.
37+
38+
Examples:
39+
3240
```scala
3341
repr global of ExecutionContext = new ForkJoinContext()
3442
repr config of Config = default.config
43+
44+
def ctx: Context
45+
repr of Context = ctx
3546
```
36-
map to
47+
would map to
3748
```scala
3849
private[this] var global$cache: ExecutionContext | Null = null
3950
final implicit def global: ExecutionContext = {
@@ -42,6 +53,8 @@ Representative clauses can be mapped to combinations of implicit objects, classe
4253
}
4354

4455
final implicit val config: Config = default.config
56+
57+
final implicit def Context_repr = ctx
4558
```
4659

4760
### Anonymous Representatives

docs/docs/reference/dropped-features/weak-conformance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ A less obvious example is the following one, which was also typed as a
2020

2121
val n: Int = 3
2222
val c: Char = 'X'
23-
val n: Double = math.sqrt(3.0)
23+
val d: Double = math.sqrt(3.0)
2424
List(n, c, d) // used to be: List[Double], now: List[AnyVal]
2525

2626
Here, it is less clear why the type should be widened to

0 commit comments

Comments
 (0)