forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support type variable definitions in quoted patterns
Support explicit type variable definition in quoted patterns. This allows users to set explicit bounds or use the binding twice. Previously this was only possible on quoted expression patterns case '{ ... }. ```scala case '[type x; x] => case '[type x; Map[x, x]] => case '[type x <: List[Any]; x] => case '[type f[X]; f] => case '[type f <: AnyKind; f] => ``` Fixes scala#10864 Fixes scala#11738
- Loading branch information
1 parent
b95b7a2
commit 2d3136f
Showing
10 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import scala.quoted._ | ||
|
||
case class T(t: Type[_]) | ||
|
||
object T { | ||
def impl[T <: AnyKind](using tt: Type[T])(using Quotes): Expr[Unit] = { | ||
val t = T(tt) | ||
t.t match | ||
case '[type x <: AnyKind; x] => // ok | ||
case _ => quotes.reflect.report.error("not ok :(") | ||
'{} | ||
} | ||
|
||
inline def run[T <: AnyKind] = ${ impl[T] } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def test = | ||
T.run[List] | ||
T.run[Map] | ||
T.run[Tuple22] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import scala.quoted._ | ||
|
||
case class T(t: Type[_]) | ||
|
||
object T { | ||
def impl[T <: AnyKind](using tt: Type[T])(using Quotes): Expr[Unit] = { | ||
val t = T(tt) | ||
t.t match | ||
case '[type x; x] => | ||
assert(Type.show[x] == "scala.Int", Type.show[x]) | ||
case '[type f[X <: Int]; f] => | ||
assert(Type.show[f] == "[T >: scala.Nothing <: scala.Int] => C[T]", Type.show[f]) | ||
case '[type f[X]; f] => | ||
assert(Type.show[f] == "[A >: scala.Nothing <: scala.Any] => scala.collection.immutable.List[A]", Type.show[f]) | ||
case '[type f <: AnyKind; f] => | ||
assert(Type.show[f] == "[K >: scala.Nothing <: scala.Any, V >: scala.Nothing <: scala.Any] => scala.collection.immutable.Map[K, V]", Type.show[f]) | ||
'{} | ||
} | ||
|
||
inline def run[T <: AnyKind] = ${ impl[T] } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@main | ||
def run = | ||
T.run[Int] | ||
T.run[C] | ||
T.run[List] | ||
T.run[Map] | ||
|
||
class C[T <: Int] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import scala.quoted.* | ||
|
||
def blah[A](using Quotes, Type[A]): Expr[Unit] = | ||
Type.of[A] match | ||
case '[h *: t] => println(s"h = ${Type.show[h]}, t = ${Type.show[t]}") // ok | ||
case '[type f[X]; f[a]] => println(s"f = ${Type.show[f]}, a = ${Type.show[a]}") // error | ||
case _ => | ||
'{()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters