Skip to content

Commit 06068ae

Browse files
committed
Docs and tests
The new implementation does not optimize given aliases with pure paths as right hand sides to be vals anymore. The reason is that even a pure path might be expensive to compute so we might want the caching. If we want a precomputed val, we can always for this with an explicit value definition: ``` val x = a.b.c given as T = x ```
1 parent 0827db2 commit 06068ae

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,21 @@ Given instances can be mapped to combinations of implicit objects, classes and i
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 givens map to implicit methods. If an alias has neither type parameters nor a given clause, its right-hand side 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.
31+
3. Alias givens map to implicit methods or implicit lazy vals. If an alias has neither type parameters nor a given clause,
32+
it is treated as a lazy val, unless the right hand side is a simple reference, in which case we can use a forwarder to that
33+
reference without caching it.
3734

3835
Examples:
3936

4037
```scala
4138
given global as ExecutionContext = new ForkJoinContext()
42-
given config as Config = default.config
4339

4440
val ctx: Context
4541
given as Context = ctx
4642
```
4743
would map to
4844
```scala
49-
private[this] var global$cache: ExecutionContext | Null = null
50-
final implicit def global: ExecutionContext = {
51-
if (global$cache == null) global$cache = new ForkJoinContext()
52-
global$cache
53-
}
54-
55-
final implicit val config: Config = default.config
56-
45+
final implicit lazy val global: ExecutionContext = new ForkJoinContext()
5746
final implicit def Context_given = ctx
5847
```
5948

tests/pos/i6909.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.compiletime
2+
trait Foo[A]
3+
4+
5+
trait Qux {
6+
given as Foo[Int] = new Foo[Int] {}
7+
}

0 commit comments

Comments
 (0)