一个更加简单的实现方式是:
package dummy
import scala.quoted.*
object Describer:
enum ShowType:
case TYPE_TREE
case TYPE_REPR
case OTHER
inline def describe[T](showType: ShowType): String = showType match
case ShowType.TYPE_TREE =>
describeTree[T]
case ShowType.TYPE_REPR =>
describeRepr[T]
case ShowType.OTHER =>
"Not Supported"
inline def describeRepr[T]: String = ${ describeReprImpl[T] }
inline def describeTree[T]: String = ${ describeTreeImpl[T] }
def describeTreeImpl[T: Type](using Quotes): Expr[String] =
import quotes.reflect.*
val tpt = TypeTree.of[T]
Expr( tpt.show(using Printer.TreeStructure) )
def describeReprImpl[T:Type] (using Quotes): Expr[String] =
import quotes.reflect.*
val tpe = TypeRepr.of[T]
Expr( tpe.dealias.widen.show(using Printer.TypeReprStructure) )
🍁 Scala3 macro & tasty 获取 TypeClass 描述信息1.概述 Scala3的Macro基于其引入的新的抽象类型结构 tasty,即:Typed Abstract Syntax Trees,其可以在编译之后,保留方法和类上的类型...