File tree Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ Shape$Circle
2
+ Shape$Circle
Original file line number Diff line number Diff line change
1
+ import scala .quoted ._
2
+ import scala .reflect .ClassTag
3
+
4
+ inline def showType [T ]: String = $ { showTypeImpl[T ] }
5
+
6
+ private def showTypeImpl [T : Type ](using Quotes ): Expr [String ] =
7
+ Expr .summon[ClassTag [T ]] match
8
+ case Some (ct) => ' { $ct.runtimeClass.getName }
9
+ case None =>
10
+ import quotes .reflect ._
11
+ report.throwError(s " Unable to find a ClassTag for type ${Type .show[T ]}" , Position .ofMacroExpansion)
Original file line number Diff line number Diff line change
1
+ enum Shape :
2
+ case Square (width : Int , height : Int ) extends Shape
3
+ case Circle (radius : Int ) extends Shape
4
+
5
+ @ main def Test : Unit =
6
+ println(showType[Shape .Circle ])
7
+ println(classOf [Shape .Circle ].getName)
8
+
You can’t perform that action at this time.
0 commit comments