Skip to content

Add string interpolator apply macro prototype #8572

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
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
34 changes: 34 additions & 0 deletions tests/run-macros/xml-interpolation-5/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import scala.quoted._
import scala.quoted.autolift

import scala.language.implicitConversions

case class Xml(parts: String, args: List[Any])

object XmlQuote {

// Encoding for
//
// implicit class SCOps(s: StringContext) {
// object xml {
// def apply(exprs: Any*) = ...
// def unapplySeq(...) = ...
// }
// }
object SCOps {
opaque type StringContext = scala.StringContext
def apply(sc: scala.StringContext): StringContext = sc
}
inline def (inline ctx: StringContext).xml <: SCOps.StringContext = SCOps(ctx)
inline def (inline ctx: SCOps.StringContext).apply(inline args: Any*): Xml =
${XmlQuote.impl('ctx, 'args)}
// inline def (inline ctx: SCOps.StringContext).unapplySeq(...): Xml = ...


def impl(receiver: Expr[SCOps.StringContext], args: Expr[Seq[Any]])(using QuoteContext): Expr[Xml] = {
val string = receiver match {
case '{ SCOps(${Unlifted(sc)}) } => sc.parts.mkString("??")
}
'{new Xml(${string}, $args.toList)}
}
}
11 changes: 11 additions & 0 deletions tests/run-macros/xml-interpolation-5/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import XmlQuote._

object Test {
def main(args: Array[String]): Unit = {

assert(xml"Hello World!" == Xml("Hello World!", Nil))

val name = new Object{}
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))
}
}
34 changes: 34 additions & 0 deletions tests/run-macros/xml-interpolation-6/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import scala.quoted._
import scala.quoted.autolift

import scala.language.implicitConversions

case class Xml(parts: String, args: List[Any])

object XmlQuote {

// Encoding for
//
// implicit class SCOps(s: StringContext) {
// object xml {
// def apply(exprs: Any*) = ...
// def unapplySeq(...) = ...
// }
// }
object SCOps {
opaque type StringContext = scala.StringContext
def apply(sc: scala.StringContext): StringContext = sc
}
inline def (inline ctx: StringContext).xml: SCOps.StringContext = SCOps(ctx)
inline def (inline ctx: SCOps.StringContext).apply(inline args: Any*): Xml =
${XmlQuote.impl('ctx, 'args)}
// inline def (inline ctx: SCOps.StringContext).unapplySeq(...): Xml = ...


def impl(receiver: Expr[SCOps.StringContext], args: Expr[Seq[Any]])(using QuoteContext): Expr[Xml] = {
val string = receiver match {
case '{ SCOps(${Unlifted(sc)}): SCOps.StringContext } => sc.parts.mkString("??")
}
'{new Xml(${string}, $args.toList)}
}
}
11 changes: 11 additions & 0 deletions tests/run-macros/xml-interpolation-6/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import XmlQuote._

object Test {
def main(args: Array[String]): Unit = {

assert(xml"Hello World!" == Xml("Hello World!", Nil))

val name = new Object{}
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))
}
}
34 changes: 34 additions & 0 deletions tests/run-macros/xml-interpolation-7/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import scala.quoted._
import scala.quoted.autolift

import scala.language.implicitConversions

case class Xml(parts: String, args: List[Any])

object XmlQuote {

// Encoding for
//
// implicit class SCOps(s: StringContext) {
// object xml {
// def apply(exprs: Any*) = ...
// def unapplySeq(...) = ...
// }
// }
object XMLOps {
opaque type StringContext = scala.StringContext
def (ctx: scala.StringContext).xml: StringContext = ctx
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liufengyun I added this other alternative that looks much simpler to use. Could you quickly check it and merge if it is ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}

inline def (inline ctx: XMLOps.StringContext).apply(inline args: Any*): Xml =
${XmlQuote.impl('ctx, 'args)}
// inline def (inline ctx: SCOps.StringContext).unapplySeq(...): Xml = ...


def impl(receiver: Expr[XMLOps.StringContext], args: Expr[Seq[Any]])(using QuoteContext): Expr[Xml] = {
val string = receiver match {
case '{ XMLOps.xml(${Unlifted(sc)}) } => sc.parts.mkString("??")
}
'{new Xml(${string}, $args.toList)}
}
}
12 changes: 12 additions & 0 deletions tests/run-macros/xml-interpolation-7/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import XmlQuote._

object Test {
def main(args: Array[String]): Unit = {
import XMLOps.xml

assert(xml"Hello World!" == Xml("Hello World!", Nil))

val name = new Object{}
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))
}
}