Search code examples
scalascala-macrosscala-3

Scala 3 TypeRepr match on higher-kinded type


How do I properly pattern match a TypeRepr on a higher-kinded type? Existentials successfully match for classes, but when I try with a type I get the compiler error unreducible application of higher-kinded type writetype.Foo to wildcard arguments

import scala.quoted.*

type Foo[X]
class Bar[X]

inline def writeType[T]: String = ${writeTypeImpl[T]}

def writeTypeImpl[T](using Type[T], Quotes): Expr[String] =
  import quotes.reflect.*
  val tpe = TypeRepr.of[T]
  val s = tpe.asType match
    //case '[Foo[?]] => "FooSuccess"
    case '[Bar[?]] => "BarSuccess"
    case _ => "Fail"

  Expr(s)

Solution

  • This is the answer you need :

    https://docs.scala-lang.org/scala3/guides/migration/incompat-other-changes.html#wildcard-type-argument

    You have to use a trait or abstract class or Foo[A] where is a concrete type (or type parameter)