# Nontrivial compile-time string interpolation pattern-matching in Scala 3

**URL:** <https://contributors.scala-lang.org/t/nontrivial-compile-time-string-interpolation-pattern-matching-in-scala-3/5295>\
**Category:** Language Design\
**Created:** [October 22, 2021, 12:42pm UTC](https://contributors.scala-lang.org/t/nontrivial-compile-time-string-interpolation-pattern-matching-in-scala-3/5295 "2021-10-22T12:42:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [October 22, 2021, 12:42pm UTC](https://contributors.scala-lang.org/t/nontrivial-compile-time-string-interpolation-pattern-matching-in-scala-3/5295/1 "2021-10-22T12:42:37Z")

</div>

If we want to do **compile-time** pattern matching checks for string interpolation in Scala 2 we write:

```scala
implicit class StrInterp(sc : StringContext) {
  object si {
    def apply(args : Any*) : Any = macro siApplyMacro
    def unapply(arg : Any): Option[Seq[Any]] = macro siUnapplyMacro
  }
}

val x = arg match {
  case si"my_pattern" => ???
}

```

In Scala 3 this is _ **nontrivial** _:

- Extension methods are limited to, well, methods, so an object cannot be placed as an extension.
- Implicit classes cannot receive an `inline` `StringContext`.

The way to do it is:

```scala
class SIParts[+P <: Tuple](parts : P): //P is a tuple representation of the SC parts
  transparent inline def apply(inline args: Any*): Any = ${siApplyMacro[P]('args, 'parts)}
  transparent inline def unapplySeq(inline arg: Any): Option[Seq[Any]] = ${siUnapplyMacro[P]('arg, `parts)}

extension (inline sc : StringContext)
  transparent inline def si : SIParts[Tuple] = ${siPartsMacro('sc)}

```

To me this seems highly burdensome and nontrivial.  
Is there a better way to do this currently?  
Is there a way to design the language better so this feature will be at least at par with Scala 2? E.g., allow extension _fields_ (`lazy val`s and `object`s) and not just methods.

---

<div class="post-metadata">

**Author:** ![mpilquist](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mpilquist/32/1910_2.png) [@mpilquist](https://contributors.scala-lang.org/u/mpilquist)\
**Post date:** [October 22, 2021, 12:54pm UTC](https://contributors.scala-lang.org/t/nontrivial-compile-time-string-interpolation-pattern-matching-in-scala-3/5295/2 "2021-10-22T12:54:18Z")

</div>

In case you haven’t seen it, I published [https://github.com/typelevel/literally](https://github.com/typelevel/literally) to help with this type of problem.

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [October 22, 2021, 1:00pm UTC](https://contributors.scala-lang.org/t/nontrivial-compile-time-string-interpolation-pattern-matching-in-scala-3/5295/3 "2021-10-22T13:00:48Z")

</div>

Nice, but that still does not solve the `unapply` problem. The `apply` is trivial and there is no need to use an object for that.

---

<div class="post-metadata">

**Author:** ![mpilquist](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mpilquist/32/1910_2.png) [@mpilquist](https://contributors.scala-lang.org/u/mpilquist)\
**Post date:** [October 22, 2021, 1:01pm UTC](https://contributors.scala-lang.org/t/nontrivial-compile-time-string-interpolation-pattern-matching-in-scala-3/5295/4 "2021-10-22T13:01:32Z")

</div>

Yep agreed, just wanted to mention it since it’s tangentially related. 🙂

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [October 23, 2021, 2:45am UTC](https://contributors.scala-lang.org/t/nontrivial-compile-time-string-interpolation-pattern-matching-in-scala-3/5295/5 "2021-10-23T02:45:09Z")

</div>

Update:  
There is an open-issue on the dotty tracker.

> <https://github.com/lampepfl/dotty/issues/8577>
>
> To write a string interpolator with an apply and unapply. Both could be macros.
> old code
> In Scala 2 we would have written
> implicit...
