Skip to content

Add documentation and missing optional seal #8958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1883,10 +1883,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
def QuotedType_unseal(self: scala.quoted.Type[?])(using ctx: Context): TypeTree =
PickledQuotes.quotedTypeToTree(self)

/** Convert `Term` to an `quoted.Expr[Any]` */
def QuotedExpr_seal(self: Term)(using ctx: Context): scala.quoted.Expr[Any] = self.tpe.widen match {
case _: Types.MethodType | _: Types.PolyType => throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.")
case _ => new TastyTreeExpr(self, compilerId)
def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]] = self.tpe.widen match {
case _: Types.MethodType | _: Types.PolyType => None
case _ => Some(new TastyTreeExpr(self, compilerId))
}

/** Checked cast to a `quoted.Expr[U]` */
Expand Down
15 changes: 14 additions & 1 deletion library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,26 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>

extension TermOps on (self: Term) {

/** Convert `Term` to an `quoted.Expr[Any]` */
/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression or throws */
def seal(using ctx: Context): scala.quoted.Expr[Any] =
internal.QuotedExpr_seal(self).getOrElse {
throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.")
}

/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression */
def sealOpt(using ctx: Context): Option[scala.quoted.Expr[Any]] =
internal.QuotedExpr_seal(self)

/** Type of this term */
def tpe(using ctx: Context): Type = internal.Term_tpe(self)

/** Replace Inlined nodes and InlineProxy references to underlying arguments */
def underlyingArgument(using ctx: Context): Term = internal.Term_underlyingArgument(self)

/** Replace Ident nodes references to the underlying tree that defined them */
def underlying(using ctx: Context): Term = internal.Term_underlying(self)

/** Converts a partally applied term into a lambda expression */
def etaExpand(using ctx: Context): Term = internal.Term_etaExpand(self)

/** A unary apply node with given argument: `tree(arg)` */
Expand Down
5 changes: 2 additions & 3 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1434,9 +1434,8 @@ trait CompilerInterface {
/** View this expression `quoted.Type[T]` as a `TypeTree` */
def QuotedType_unseal(self: scala.quoted.Type[_])(using ctx: Context): TypeTree

/** Convert `Term` to an `quoted.Expr[Any]` */
def QuotedExpr_seal(self: Term)(using ctx: Context): scala.quoted.Expr[Any]

/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression */
def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]]

/** Convert `Type` to an `quoted.Type[_]` */
def QuotedType_seal(self: Type)(using ctx: Context): scala.quoted.Type[_]
Expand Down