一個(gè)更加簡(jiǎn)單的實(shí)現(xiàn)方式是:
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基于其引入的新的抽象類型結(jié)構(gòu) tasty,即:Typed Abstract Syntax Trees,其可以在編譯之后正什,保留方法和類上的類型...