# Dotty macro: How to add a type bound to a type match?

**URL:** <https://users.scala-lang.org/t/dotty-macro-how-to-add-a-type-bound-to-a-type-match/9846>\
**Category:** Question\
**Created:** [February 29, 2024, 1:37pm UTC](https://users.scala-lang.org/t/dotty-macro-how-to-add-a-type-bound-to-a-type-match/9846 "2024-02-29T13:37:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmf](https://users.scala-lang.org/user_avatar/users.scala-lang.org/hmf/32/426_2.png) [@hmf](https://users.scala-lang.org/u/hmf)\
**Post date:** [February 29, 2024, 1:37pm UTC](https://users.scala-lang.org/t/dotty-macro-how-to-add-a-type-bound-to-a-type-match/9846/1 "2024-02-29T13:37:35Z")

</div>

I am trying to adapt the [quote types pattern matching example](https://docs.scala-lang.org/scala3/guides/macros/quotes.html#quote-types-patterns) so that I match a `Tuple` of `Tuple2`, wherein the first element of `Tuple2` must be a Singleton. This is what I have:

```auto
  def mirrorMapSingleImpl[T: Type](using Quotes): Expr[List[(String, String)]] =

    def rec[A : Type]: List[(String, String)] = 
      Type.of[A] match
        case '[Tuple2[name <: Singleton, value] *: fields] =>
          (Type.show[name], Type.show[value]) :: rec[fields]
        case '[EmptyTuple] =>
          Nil
        case _ =>
          quotes.reflect.report.errorAndAbort("Expected known tuple but got: " + Type.show[A])

    Expr(rec)

```

If I remove the `<: Singleton` bounds it works. If not, I get the following error:

```shell
[error] -- [E040] Syntax Error: /home/hmf/VSCodeProjects/sploty/meta/src/examples/tips_and_tricks/MTypes.scala:84:27 
[error] 84 | case '[Tuple2[name <: Singleton, value] *: fields] =>
[error] | ^^
[error] | ']' expected, but '<:' found
[error] -- [E006] Not Found Error: /home/hmf/VSCodeProjects/sploty/meta/src/examples/tips_and_tricks/MTypes.scala:85:38 
[error] 85 | (Type.show[name], Type.show[value]) :: rec[fields]
[error] | ^^^^^
[error] | Not found: type value
[error] |----------------------------------------------------------------------------
[error] | Explanation (enabled by `-explain`)
[error] |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error] | The identifier for `type value` is not bound, that is,
[error] | no declaration for this identifier can be found.
[error] | That can happen, for example, if `value` or its declaration has either been
[error] | misspelt or if an import is missing.
[error] ----------------------------------------------------------------------------
[error] Explanation
[error] ===========
[error] The identifier for `type value` is not bound, that is,
[error] no declaration for this identifier can be found.
[error] That can happen, for example, if `value` or its declaration has either been
[error] misspelt or if an import is missing.
[error] two errors found
1 targets failed

```

So it seems that I have to declare a:

```scala
type name <: Singleton

```

How do I do this? Documentation only shows this within quotes.

TIA

---

<div class="post-metadata">

**Author:** ![hmf](https://users.scala-lang.org/user_avatar/users.scala-lang.org/hmf/32/426_2.png) [@hmf](https://users.scala-lang.org/u/hmf)\
**Post date:** [March 3, 2024, 11:39am UTC](https://users.scala-lang.org/t/dotty-macro-how-to-add-a-type-bound-to-a-type-match/9846/2 "2024-03-03T11:39:52Z")

</div>

Seems to be available only from 3.4.0. See:

> <https://github.com/lampepfl/dotty/pull/16910>
>
> Support explicit type variable definition in quoted patterns. This allows users …to set explicit bounds or use the binding twice.
> 
> \`\`\`scala
> case '\[type x; \`x\`\] =\>
> case '\[type x; Map\[\`x\`, \`x\`\]\] =\>
> case '\[type x \<: List\[Any\]; \`x\`\] =\>
> case '\[type f\[X\]; \`f\`\] =\>
> case '\[type f \<: AnyKind; \`f\`\] =\>
> \`\`\`
> 
> Previously this was only possible on quoted expression patterns \`case '{ ... }\`. We also had no way to set bounds for type variables in quoted type patterns.
> 
> 
> Fixes #10864
> Fixes #11738
> 
> SIP: https://github.com/scala/improvement-proposals/pull/59
