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

I am trying to adapt the quote types pattern matching example so that I match a Tuple of Tuple2, wherein the first element of Tuple2 must be a Singleton. This is what I have:

  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:

[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:

type name <: Singleton

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

TIA

1 Like

Seems to be available only from 3.4.0. See:

1 Like