-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add minimal prototype for f interpolator macro #5532
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import scala.quoted._ | ||
import scala.tasty.Reflection | ||
|
||
import scala.language.implicitConversions | ||
|
||
object FQuote { | ||
|
||
implicit class SCOps(ctx: StringContext) { | ||
inline def ff(args: => Any*): String = ~impl('(this), '(args)) | ||
} | ||
|
||
/*private*/ def impl(receiver: Expr[SCOps], args: Expr[Seq[Any]])(implicit reflect: Reflection): Expr[String] = { | ||
import reflect._ | ||
|
||
def liftListOfAny(lst: List[Term]): Expr[List[Any]] = lst match { | ||
case x :: xs => | ||
val head = x.seal[Any] | ||
val tail = liftListOfAny(xs) | ||
'{ ~head :: ~tail } | ||
case Nil => '(Nil) | ||
} | ||
|
||
def isStringConstant(tree: Term) = tree match { | ||
case Term.Literal(_) => true | ||
case _ => false | ||
} | ||
|
||
def isSCOpsConversion(tree: Term) = | ||
tree.symbol.fullName == "FQuote$.SCOps" | ||
|
||
def isStringContextApply(tree: Term) = | ||
tree.symbol.fullName == "scala.StringContext$.apply" | ||
|
||
// FQuote.SCOps(StringContext.apply([p0, ...]: String*) | ||
val parts = receiver.unseal.underlyingArgument match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For later: it seems the name |
||
case Term.Apply(conv, List(Term.Apply(fun, List(Term.Typed(Term.Repeated(values), _))))) | ||
if isSCOpsConversion(conv) && | ||
isStringContextApply(fun) && | ||
values.forall(isStringConstant) => | ||
values.collect { case Term.Literal(Constant.String(value)) => value } | ||
case tree => | ||
throw new QuoteError(s"String literal expected, but ${tree.show} found") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For later: better error message with positions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just an example for the student to start |
||
} | ||
|
||
// [a0, ...]: Any* | ||
val Term.Typed(Term.Repeated(allArgs), _) = args.unseal.underlyingArgument | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So would it be possible to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I copied this basic implementation from an old version of the xml interpolator. There are many other improvements that can be done. This is just a simple example of one way to do it. |
||
|
||
for ((arg, part) <- allArgs.zip(parts.tail)) { | ||
if (part.startsWith("%d") && !(arg.tpe <:< definitions.IntType)) { | ||
return '(s"`${~arg.showCode.toExpr}` is not of type Int") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error message instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just an example for the student to start. It is a simple way to see the output. |
||
} | ||
|
||
} | ||
|
||
val string = parts.mkString("") | ||
'{ new collection.immutable.StringOps(~string.toExpr).format(~args: _*) } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import FQuote._ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val one: Int = 1 | ||
assert(ff"Hello $one%d!" == "Hello 1!") | ||
val world: String = "world" | ||
assert(ff"Hello $world%d!" == "`world` is not of type Int") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm: I remember some time ago, we need to use
=> StringContext
. I guess withunderlyingArgument
this is no longer required.