Skip to content

Commit 3a502e5

Browse files
committed
Add string interpolator apply macro prototype
This is just an example of how to encode String interpolator macros that used an object with and `apply` and `unapply` in the extension class.
1 parent d9df0a2 commit 3a502e5

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import scala.quoted._
2+
import scala.quoted.autolift
3+
4+
import scala.language.implicitConversions
5+
6+
case class Xml(parts: String, args: List[Any])
7+
8+
object XmlQuote {
9+
10+
// Encoding for
11+
//
12+
// implicit class SCOps(s: StringContext) {
13+
// object xml {
14+
// def apply(exprs: Any*) = ...
15+
// def unapplySeq(...) = ...
16+
// }
17+
// }
18+
object SCOps {
19+
opaque type StringContext = scala.StringContext
20+
def apply(sc: scala.StringContext): StringContext = sc
21+
}
22+
inline def (inline ctx: StringContext).xml <: SCOps.StringContext = SCOps(ctx)
23+
inline def (inline ctx: SCOps.StringContext).apply(inline args: Any*): Xml =
24+
${XmlQuote.impl('ctx, 'args)}
25+
// inline def (inline ctx: SCOps.StringContext).unapplySeq(...): Xml = ...
26+
27+
28+
def impl(receiver: Expr[SCOps.StringContext], args: Expr[Seq[Any]])(using QuoteContext): Expr[Xml] = {
29+
val string = receiver match {
30+
case '{ SCOps(${Unlifted(sc)}) } => sc.parts.mkString("??")
31+
}
32+
'{new Xml(${string}, $args.toList)}
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import XmlQuote._
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
6+
assert(xml"Hello World!" == Xml("Hello World!", Nil))
7+
8+
val name = new Object{}
9+
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))
10+
}
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import scala.quoted._
2+
import scala.quoted.autolift
3+
4+
import scala.language.implicitConversions
5+
6+
case class Xml(parts: String, args: List[Any])
7+
8+
object XmlQuote {
9+
10+
// Encoding for
11+
//
12+
// implicit class SCOps(s: StringContext) {
13+
// object xml {
14+
// def apply(exprs: Any*) = ...
15+
// def unapplySeq(...) = ...
16+
// }
17+
// }
18+
object SCOps {
19+
opaque type StringContext = scala.StringContext
20+
def apply(sc: scala.StringContext): StringContext = sc
21+
}
22+
inline def (inline ctx: StringContext).xml: SCOps.StringContext = SCOps(ctx)
23+
inline def (inline ctx: SCOps.StringContext).apply(inline args: Any*): Xml =
24+
${XmlQuote.impl('ctx, 'args)}
25+
// inline def (inline ctx: SCOps.StringContext).unapplySeq(...): Xml = ...
26+
27+
28+
def impl(receiver: Expr[SCOps.StringContext], args: Expr[Seq[Any]])(using QuoteContext): Expr[Xml] = {
29+
val string = receiver match {
30+
case '{ SCOps(${Unlifted(sc)}): SCOps.StringContext } => sc.parts.mkString("??")
31+
}
32+
'{new Xml(${string}, $args.toList)}
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import XmlQuote._
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
6+
assert(xml"Hello World!" == Xml("Hello World!", Nil))
7+
8+
val name = new Object{}
9+
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))
10+
}
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import scala.quoted._
2+
import scala.quoted.autolift
3+
4+
import scala.language.implicitConversions
5+
6+
case class Xml(parts: String, args: List[Any])
7+
8+
object XmlQuote {
9+
10+
// Encoding for
11+
//
12+
// implicit class SCOps(s: StringContext) {
13+
// object xml {
14+
// def apply(exprs: Any*) = ...
15+
// def unapplySeq(...) = ...
16+
// }
17+
// }
18+
object XMLOps {
19+
opaque type StringContext = scala.StringContext
20+
def (ctx: scala.StringContext).xml: StringContext = ctx
21+
}
22+
23+
inline def (inline ctx: XMLOps.StringContext).apply(inline args: Any*): Xml =
24+
${XmlQuote.impl('ctx, 'args)}
25+
// inline def (inline ctx: SCOps.StringContext).unapplySeq(...): Xml = ...
26+
27+
28+
def impl(receiver: Expr[XMLOps.StringContext], args: Expr[Seq[Any]])(using QuoteContext): Expr[Xml] = {
29+
val string = receiver match {
30+
case '{ XMLOps.xml(${Unlifted(sc)}) } => sc.parts.mkString("??")
31+
}
32+
'{new Xml(${string}, $args.toList)}
33+
}
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import XmlQuote._
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
import XMLOps.xml
6+
7+
assert(xml"Hello World!" == Xml("Hello World!", Nil))
8+
9+
val name = new Object{}
10+
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))
11+
}
12+
}

0 commit comments

Comments
 (0)