You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fail(indentity("foo")) // error: failed on: indentity("foo")
407
407
```
408
408
409
-
## Implicit Matches
409
+
## Summoning Implicits Selectively
410
410
411
411
It is foreseen that many areas of typelevel programming can be done with rewrite
412
412
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
441
441
instance by allowing more elaborate schemes to specify priorities. But they all
442
442
keep the viral nature of implicit search programs based on logic programming.
443
443
444
-
By contrast, the new `implicit match` construct makes implicit search available
444
+
By contrast, the new `summonFrom` construct makes implicit search available
445
445
in a functional context. To solve the problem of creating the right set, one
446
446
would use it as follows:
447
447
```scala
448
-
inlinedefsetFor[T]:Set[T] =implicitmatch {
449
-
caseord: Ordering[T] =>newTreeSet[T]
450
-
case _ =>newHashSet[T]
448
+
importscala.compiletime.summonFrom
449
+
450
+
inlinedefsetFor[T]:Set[T] = summonFrom match {
451
+
casegivenord:Ordering[T] =>newTreeSet[T]
452
+
case _ =>newHashSet[T]
451
453
}
452
454
```
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`.
455
457
456
458
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.
461
461
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.
465
463
466
464
Consequently, if we summon an `Ordering[String]` the code above will return a
467
465
new instance of `TreeSet[String]`.
@@ -472,7 +470,7 @@ the[Ordering[String]]
472
470
println(setFor[String].getClass) // prints class scala.collection.immutable.TreeSet
473
471
```
474
472
475
-
**Note**implicit matches can raise ambiguity errors. Consider the following
473
+
**Note**`summonFrom` applications can raise ambiguity errors. Consider the following
476
474
code with two implicit values in scope of type `A`. The single pattern match
477
475
case of the implicit match with type ascription of an `A` raises the ambiguity
478
476
error.
@@ -482,13 +480,12 @@ class A
482
480
implicitvala1:A=newA
483
481
implicitvala2:A=newA
484
482
485
-
inlinedeff:Any=implicitmatch {
483
+
inlinedeff:Any=summonFrom {
486
484
case_: A=>???// error: ambiguous implicits
487
485
}
488
486
```
489
487
490
488
### Reference
491
489
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