Skip to content

Commit b049b77

Browse files
committed
Modify reference docs
1 parent d4e6cbd commit b049b77

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

docs/docs/reference/metaprogramming/inline.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ inline def fail(p1: => Any) = {
406406
fail(indentity("foo")) // error: failed on: indentity("foo")
407407
```
408408

409-
## Implicit Matches
409+
## Summoning Implicits Selectively
410410

411411
It is foreseen that many areas of typelevel programming can be done with rewrite
412412
methods instead of implicits. But sometimes implicits are unavoidable. The
@@ -441,27 +441,25 @@ There are some proposals to improve the situation in specific areas, for
441441
instance by allowing more elaborate schemes to specify priorities. But they all
442442
keep the viral nature of implicit search programs based on logic programming.
443443

444-
By contrast, the new `implicit match` construct makes implicit search available
444+
By contrast, the new `summonFrom` construct makes implicit search available
445445
in a functional context. To solve the problem of creating the right set, one
446446
would use it as follows:
447447
```scala
448-
inline def setFor[T]: Set[T] = implicit match {
449-
case ord: Ordering[T] => new TreeSet[T]
450-
case _ => new HashSet[T]
448+
import scala.compiletime.summonFrom
449+
450+
inline def setFor[T]: Set[T] = summonFrom match {
451+
case given ord: Ordering[T] => new TreeSet[T]
452+
case _ => new HashSet[T]
451453
}
452454
```
453-
An implicit match uses the `implicit` keyword in the place of the scrutinee. Its
454-
patterns are type ascriptions of the form `identifier : Type`.
455+
A `summonFrom` call takes a pattern matching closure as argument. All patterns
456+
in the closure are type ascriptions of the form `identifier : Type`.
455457

456458
Patterns are tried in sequence. The first case with a pattern `x: T` such that
457-
an implicit value of type `T` can be summoned is chosen. The variable `x` is
458-
then bound to the implicit value for the remainder of the case. It can in turn
459-
be used as an implicit in the right hand side of the case. It is an error if one
460-
of the tested patterns gives rise to an ambiguous implicit search.
459+
an implicit value of type `T` can be summoned is chosen. If the pattern is prefixed
460+
with `given`, the variable `x` is bound to the implicit value for the remainder of the case. It can in turn be used as an implicit in the right hand side of the case. It is an error if one of the tested patterns gives rise to an ambiguous implicit search.
461461

462-
An implicit matches is considered to be a special kind of a inline match. This
463-
means it can only occur in the body of an inline method, and it must be reduced
464-
at compile time.
462+
`summonFrom` applications must be reduced at compile time.
465463

466464
Consequently, if we summon an `Ordering[String]` the code above will return a
467465
new instance of `TreeSet[String]`.
@@ -472,7 +470,7 @@ the[Ordering[String]]
472470
println(setFor[String].getClass) // prints class scala.collection.immutable.TreeSet
473471
```
474472

475-
**Note** implicit matches can raise ambiguity errors. Consider the following
473+
**Note** `summonFrom` applications can raise ambiguity errors. Consider the following
476474
code with two implicit values in scope of type `A`. The single pattern match
477475
case of the implicit match with type ascription of an `A` raises the ambiguity
478476
error.
@@ -482,13 +480,12 @@ class A
482480
implicit val a1: A = new A
483481
implicit val a2: A = new A
484482

485-
inline def f: Any = implicit match {
483+
inline def f: Any = summonFrom {
486484
case _: A => ??? // error: ambiguous implicits
487485
}
488486
```
489487

490488
### Reference
491489

492-
For more info, see [PR #4927](https://github.com/lampepfl/dotty/pull/4768),
493-
which explains how inline methods can be used for typelevel programming and code
494-
specialization.
490+
For more info, see [PR #4768](https://github.com/lampepfl/dotty/pull/4768),
491+
which explains how `summonFrom`'s predecessor (implicit matches) can be used for typelevel programming and code specialization and [PR #7201](https://github.com/lampepfl/dotty/pull/7201) which explains the new `summonFrom` syntax.

0 commit comments

Comments
 (0)