Skip to content

Commit a6ecc14

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 4d2f216 commit a6ecc14

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
@@ -1683,11 +1683,8 @@ object Parsers {
16831683

16841684
def expr(location: Location.Value): Tree = {
16851685
val start = in.offset
1686-
if (closureMods.contains(in.token)) {
1687-
val imods = modifiers(closureMods)
1688-
if (in.token == MATCH) impliedMatch(start, imods)
1689-
else implicitClosure(start, location, imods)
1690-
}
1686+
if (closureMods.contains(in.token))
1687+
implicitClosure(start, location, modifiers(closureMods))
16911688
else {
16921689
val saved = placeholderParams
16931690
placeholderParams = Nil
@@ -1889,30 +1886,6 @@ object Parsers {
18891886
}
18901887
}
18911888

1892-
/** `match' { ImplicitCaseClauses }
1893-
*/
1894-
def impliedMatch(start: Int, imods: Modifiers) = {
1895-
def markFirstIllegal(mods: List[Mod]) = mods match {
1896-
case mod :: _ => syntaxError(em"illegal modifier for given match", mod.span)
1897-
case _ =>
1898-
}
1899-
imods.mods match {
1900-
case (Mod.Implicit() | Mod.Given()) :: mods => markFirstIllegal(mods)
1901-
case mods => markFirstIllegal(mods)
1902-
}
1903-
val result @ Match(t, cases) =
1904-
matchExpr(EmptyTree, start, InlineMatch)
1905-
for (CaseDef(pat, _, _) <- cases) {
1906-
def isImplicitPattern(pat: Tree) = pat match {
1907-
case Typed(pat1, _) => isVarPattern(pat1)
1908-
case pat => isVarPattern(pat)
1909-
}
1910-
if (!isImplicitPattern(pat))
1911-
syntaxError(em"not a legal pattern for a given match", pat.span)
1912-
}
1913-
result
1914-
}
1915-
19161889
/** `match' { TypeCaseClauses }
19171890
*/
19181891
def matchType(t: Tree): MatchTypeTree =
@@ -3754,8 +3727,6 @@ object Parsers {
37543727
var imods = modifiers(closureMods)
37553728
if (isBindingIntro)
37563729
stats += implicitClosure(start, Location.InBlock, imods)
3757-
else if (in.token == MATCH)
3758-
stats += impliedMatch(start, imods)
37593730
else
37603731
stats +++= localDef(start, imods)
37613732
}

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)