Skip to content

Commit 6662892

Browse files
committed
Bootstrap quoted.Type and tasty.reflect.QuotedOps
1 parent e0449db commit 6662892

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package scala.quoted
2+
3+
import scala.quoted.Types.TaggedType
4+
import scala.reflect.ClassTag
5+
import scala.runtime.quoted.Unpickler.Pickled
6+
7+
sealed abstract class Type[T] {
8+
type `$splice` = T
9+
}
10+
11+
/** Some basic type tags, currently incomplete */
12+
object Type {
13+
/** A term quote is desugared by the compiler into a call to this method */
14+
def apply[T]: Type[T] =
15+
throw new Error("Internal error: this method call should have been replaced by the compiler")
16+
17+
implicit def UnitTag: Type[Unit] = new TaggedType[Unit]
18+
implicit def BooleanTag: Type[Boolean] = new TaggedType[Boolean]
19+
implicit def ByteTag: Type[Byte] = new TaggedType[Byte]
20+
implicit def CharTag: Type[Char] = new TaggedType[Char]
21+
implicit def ShortTag: Type[Short] = new TaggedType[Short]
22+
implicit def IntTag: Type[Int] = new TaggedType[Int]
23+
implicit def LongTag: Type[Long] = new TaggedType[Long]
24+
implicit def FloatTag: Type[Float] = new TaggedType[Float]
25+
implicit def DoubleTag: Type[Double] = new TaggedType[Double]
26+
}
27+
28+
/** All implementations of Type[T].
29+
* These should never be used directly.
30+
*/
31+
object Types {
32+
/** A Type backed by a pickled TASTY tree */
33+
final class TastyType[T](val tasty: Pickled, val args: Seq[Any]) extends Type[T] {
34+
override def toString(): String = s"Type(<pickled tasty>)"
35+
}
36+
37+
/** An Type backed by a value */
38+
final class TaggedType[T](implicit val ct: ClassTag[T]) extends Type[T] {
39+
override def toString: String = s"Type($ct)"
40+
}
41+
42+
/** An Type backed by a tree */
43+
final class TreeType[Tree](val typeTree: Tree) extends quoted.Type[Any] {
44+
override def toString: String = s"Type(<tasty tree>)"
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package scala.tasty.reflect
2+
3+
/** Extension methods on scala.quoted.{Expr|Type} to convert to scala.tasty.Tasty objects */
4+
trait QuotedOps extends Core {
5+
6+
implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) {
7+
/** View this expression `Expr[T]` as a `Term` */
8+
def unseal(implicit ctx: Context): Term =
9+
kernel.QuotedExpr_unseal(expr)
10+
}
11+
12+
implicit class QuotedTypeAPI[T](tpe: scala.quoted.Type[T]) {
13+
/** View this expression `Type[T]` as a `TypeTree` */
14+
def unseal(implicit ctx: Context): TypeTree =
15+
kernel.QuotedType_unseal(tpe)
16+
}
17+
18+
implicit class TermToQuotedAPI(term: Term) {
19+
/** Convert `Term` to an `Expr[T]` and check that it conforms to `T` */
20+
def seal[T](implicit tpe: scala.quoted.Type[T], ctx: Context): scala.quoted.Expr[T] =
21+
kernel.QuotedExpr_seal(term)(tpe)
22+
}
23+
24+
implicit class TypeToQuotedAPI(tpe: Type) {
25+
/** Convert `Type` to an `quoted.Type[T]` */
26+
def seal(implicit ctx: Context): scala.quoted.Type[_] =
27+
kernel.QuotedType_seal(tpe)
28+
}
29+
}

0 commit comments

Comments
 (0)