Skip to content

Commit faba4c9

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 60c3a12 commit faba4c9

File tree

10 files changed

+21
-47
lines changed

10 files changed

+21
-47
lines changed

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

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

16421642
def expr(location: Location.Value): Tree = {
16431643
val start = in.offset
1644-
if (closureMods.contains(in.token)) {
1645-
val imods = modifiers(closureMods)
1646-
if (in.token == MATCH) impliedMatch(start, imods)
1647-
else implicitClosure(start, location, imods)
1648-
}
1644+
if (closureMods.contains(in.token))
1645+
implicitClosure(start, location, modifiers(closureMods))
16491646
else {
16501647
val saved = placeholderParams
16511648
placeholderParams = Nil
@@ -1847,30 +1844,6 @@ object Parsers {
18471844
}
18481845
}
18491846

1850-
/** `match' { ImplicitCaseClauses }
1851-
*/
1852-
def impliedMatch(start: Int, imods: Modifiers) = {
1853-
def markFirstIllegal(mods: List[Mod]) = mods match {
1854-
case mod :: _ => syntaxError(em"illegal modifier for given match", mod.span)
1855-
case _ =>
1856-
}
1857-
imods.mods match {
1858-
case (Mod.Implicit() | Mod.Given()) :: mods => markFirstIllegal(mods)
1859-
case mods => markFirstIllegal(mods)
1860-
}
1861-
val result @ Match(t, cases) =
1862-
matchExpr(EmptyTree, start, InlineMatch)
1863-
for (CaseDef(pat, _, _) <- cases) {
1864-
def isImplicitPattern(pat: Tree) = pat match {
1865-
case Typed(pat1, _) => isVarPattern(pat1)
1866-
case pat => isVarPattern(pat)
1867-
}
1868-
if (!isImplicitPattern(pat))
1869-
syntaxError(em"not a legal pattern for a given match", pat.span)
1870-
}
1871-
result
1872-
}
1873-
18741847
/** `match' { TypeCaseClauses }
18751848
*/
18761849
def matchType(t: Tree): MatchTypeTree =
@@ -3664,8 +3637,6 @@ object Parsers {
36643637
var imods = modifiers(closureMods)
36653638
if (isBindingIntro)
36663639
stats += implicitClosure(start, Location.InBlock, imods)
3667-
else if (in.token == MATCH)
3668-
stats += impliedMatch(start, imods)
36693640
else
36703641
stats +++= localDef(start, imods)
36713642
}

docs/docs/reference/contextual/derivation.md

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

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

167167

docs/docs/reference/metaprogramming/inline.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,16 @@ println(setFor[String].getClass) // prints class scala.collection.immutable.Tree
471471
```
472472

473473
**Note** `summonFrom` applications can raise ambiguity errors. Consider the following
474-
code with two implicit values in scope of type `A`. The single pattern match
475-
case of the implicit match with type ascription of an `A` raises the ambiguity
476-
error.
474+
code with two implicit values in scope of type `A`. The pattern match in `f` will raise
475+
an ambiguity error of `f` is applied.
477476

478477
```scala
479478
class A
480479
implicit val a1: A = new A
481480
implicit val a2: A = new A
482481

483482
inline def f: Any = summonFrom {
484-
case _: A => ??? // error: ambiguous implicits
483+
case given _: A => ??? // error: ambiguous implicits
485484
}
486485
```
487486

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)