Skip to content

Commit 0c00c3b

Browse files
committed
Drop implicit match and given match
Drop `given match` and `implict match` syntax. The docs on typeclass derivation still have to be updated to conform to the new terminology.
1 parent 421d2e9 commit 0c00c3b

File tree

10 files changed

+23
-49
lines changed

10 files changed

+23
-49
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,11 +1648,8 @@ object Parsers {
16481648

16491649
def expr(location: Location.Value): Tree = {
16501650
val start = in.offset
1651-
if (closureMods.contains(in.token)) {
1652-
val imods = modifiers(closureMods)
1653-
if (in.token == MATCH) impliedMatch(start, imods)
1654-
else implicitClosure(start, location, imods)
1655-
}
1651+
if (closureMods.contains(in.token))
1652+
implicitClosure(start, location, modifiers(closureMods))
16561653
else {
16571654
val saved = placeholderParams
16581655
placeholderParams = Nil
@@ -1854,30 +1851,6 @@ object Parsers {
18541851
}
18551852
}
18561853

1857-
/** `match' { ImplicitCaseClauses }
1858-
*/
1859-
def impliedMatch(start: Int, imods: Modifiers) = {
1860-
def markFirstIllegal(mods: List[Mod]) = mods match {
1861-
case mod :: _ => syntaxError(em"illegal modifier for given match", mod.span)
1862-
case _ =>
1863-
}
1864-
imods.mods match {
1865-
case (Mod.Implicit() | Mod.Given()) :: mods => markFirstIllegal(mods)
1866-
case mods => markFirstIllegal(mods)
1867-
}
1868-
val result @ Match(t, cases) =
1869-
matchExpr(EmptyTree, start, InlineMatch)
1870-
for (CaseDef(pat, _, _) <- cases) {
1871-
def isImplicitPattern(pat: Tree) = pat match {
1872-
case Typed(pat1, _) => isVarPattern(pat1)
1873-
case pat => isVarPattern(pat)
1874-
}
1875-
if (!isImplicitPattern(pat))
1876-
syntaxError(em"not a legal pattern for a given match", pat.span)
1877-
}
1878-
result
1879-
}
1880-
18811854
/** `match' { TypeCaseClauses }
18821855
*/
18831856
def matchType(t: Tree): MatchTypeTree =
@@ -3673,8 +3646,6 @@ object Parsers {
36733646
var imods = modifiers(closureMods)
36743647
if (isBindingIntro)
36753648
stats += implicitClosure(start, Location.InBlock, imods)
3676-
else if (in.token == MATCH)
3677-
stats += impliedMatch(start, imods)
36783649
else
36793650
stats +++= localDef(start, imods)
36803651
}

docs/docs/reference/contextual/derivation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ worked out example of such a library, see [shapeless 3](https://github.com/miles
165165
#### How to write a type class `derived` method using low level mechanisms
166166

167167
The low-level method we will use to implement a type class `derived` method in this example exploits three new
168-
type-level constructs in Dotty: inline methods, inline matches, and given matches. Given this definition of the
168+
type-level constructs in Dotty: inline methods, inline matches, and implicit searches via `summonFrom`. Given this definition of the
169169
`Eq` type class,
170170

171171

docs/docs/reference/metaprogramming/inline.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,18 +475,17 @@ summon[Ordering[String]]
475475
println(setFor[String].getClass) // prints class scala.collection.immutable.TreeSet
476476
```
477477

478-
**Note** implicit matches can raise ambiguity errors. Consider the following
479-
code with two implicit values in scope of type `A`. The single pattern match
480-
case of the implicit match with type ascription of an `A` raises the ambiguity
481-
error.
478+
**Note** `summonFrom` applications can raise ambiguity errors. Consider the following
479+
code with two implicit values in scope of type `A`. The pattern match in `f` will raise
480+
an ambiguity error of `f` is applied.
482481

483482
```scala
484483
class A
485484
implicit val a1: A = new A
486485
implicit val a2: A = new A
487486

488-
inline def f: Any = implicit match {
489-
case _: A => ??? // error: ambiguous implicits
487+
inline def f: Any = summonFrom {
488+
case given _: A => ??? // error: ambiguous implicits
490489
}
491490
```
492491

docs/docs/reference/metaprogramming/macros.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ sum
571571

572572
### Find implicits within a macro
573573

574-
Similarly to the `implicit match` construct, it is possible to make implicit search available
574+
Similarly to the `summonFrom` construct, it is possible to make implicit search available
575575
in a quote context. For this we simply provide `scala.quoted.matching.searchImplicitExpr:
576576

577577
```scala

library/src/dotty/DottyPredef.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dotty
22

33
object DottyPredef {
4+
import compiletime.summonFrom
45

56
@forceInline final def assert(assertion: => Boolean, message: => Any): Unit = {
67
if (!assertion)
@@ -32,7 +33,7 @@ object DottyPredef {
3233
* }}}
3334
* @group utilities
3435
*/
35-
inline def valueOf[T]: T = implicit match {
36+
inline def valueOf[T]: T = summonFrom {
3637
case ev: ValueOf[T] => ev.value
3738
}
3839

library/src/scala/compiletime/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ package object compiletime {
5050
*
5151
* the returned value would be `2`.
5252
*/
53-
inline def summonFrom(f: Nothing => Any) <: Any = ???
53+
inline def summonFrom[T](f: Nothing => T) <: T = ???
5454

5555
type S[X <: Int] <: Int
5656
}

tests/neg/machine-state-encoding-with-implicit-match.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import scala.annotation.implicitNotFound
2+
import scala.compiletime.summonFrom
23

34
sealed trait State
45
final class On extends State
@@ -17,10 +18,10 @@ object IsOn {
1718
}
1819

1920
class Machine[S <: State] {
20-
inline def turnOn() given (s: IsOff[S]) <: Machine[On] = implicit match {
21+
inline def turnOn() given (s: IsOff[S]) <: Machine[On] = summonFrom {
2122
case _: IsOff[Off] => new Machine[On]
2223
}
23-
inline def turnOff() given (s: IsOn[S]) <: Machine[Off] = implicit match {
24+
inline def turnOff() given (s: IsOn[S]) <: Machine[Off] = summonFrom {
2425
case _: IsOn[On] => new Machine[Off]
2526
}
2627
}

tests/pos/scala-days-2019-slides/metaprogramming-1-forset.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
object ForSetExample {
22

33
import scala.collection.immutable._
4+
import scala.compiletime.summonFrom
45

56
inline def setFor[T]: Set[T] =
6-
implicit match {
7+
summonFrom {
78
case ord: Ordering[T] => new TreeSet[T]
89
case _ => new HashSet[T]
910
}

tests/run/implicitMatch.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
object Test extends App {
22
import collection.immutable.TreeSet
33
import collection.immutable.HashSet
4+
import compiletime.summonFrom
45

5-
inline def f1[T]() = implicit match {
6+
inline def f1[T]() = summonFrom {
67
case ord: Ordering[T] => new TreeSet[T]
78
case _ => new HashSet[T]
89
}
910

10-
inline def f2[T]() = implicit match {
11+
inline def f2[T]() = summonFrom {
1112
case _: Ordering[T] => new TreeSet[T]
1213
case _ => new HashSet[T]
1314
}
@@ -16,7 +17,7 @@ object Test extends App {
1617
class B
1718
implicit val b: B = new B
1819

19-
inline def g = implicit match {
20+
inline def g = summonFrom {
2021
case _: A => println("A")
2122
case _: B => println("B")
2223
}

tests/run/typeclass-derivation-doc-example.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import scala.deriving._
2-
import scala.compiletime.erasedValue
2+
import scala.compiletime.{erasedValue, summonFrom}
33

4-
inline def summon[T]: T = given match {
4+
inline def summon[T]: T = summonFrom {
55
case t: T => t
66
}
77

0 commit comments

Comments
 (0)