Scala 3 TypeRepr 匹配更高级的类型

标签 scala scala-macros scala-3

如何正确模式匹配 TypeRepr在更高级的类型上? Existentials 成功匹配类,但是当我尝试使用类型时,我收到编译器错误 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)

最佳答案

这是您需要的答案:
https://docs.scala-lang.org/scala3/guides/migration/incompat-other-changes.html#wildcard-type-argument
您必须使用 traitabstract classFoo[A]其中是具体类型(或类型参数)

关于Scala 3 TypeRepr 匹配更高级的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67961353/

相关文章:

eclipse - maven-scala-plugin 给出 pom 文件错误

scala - 带字符串内部类型的值类?

scala - 需要宏 : found: singleton type with underlying type A, 的奇怪类型不匹配:A

scala - dotty/scala3 与 scala-native 和 scala-js 等技术的集成有多无缝?

scala - 自动化路由的 REST API 文档

scala - 如何在编译时要求类型参数是特征(而不是类或其他类型值)?

scala - 如何确定传递给宏的表达式是否始终产生相同的值?

scala - 如何将两个元组与兼容的类型结合起来?

scala - Scala 3 中的递归高阶函数类型

Java/Kotlin- Akka Stream Source.reduce 在 Source 中为 null 时不起作用