Last active
September 3, 2024 03:22
-
-
Save nagat01/b580994a8a2863633e54fa157effd6f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // macro.scala | |
| import scala.quoted.* | |
| trait Show[T]: | |
| def show(value: T): String | |
| inline given showInstance[T]: Show[T] = ${ showInstanceImpl[T] } | |
| def showInstanceImpl[T: Type](using Quotes) = '{ | |
| new Show[T] { | |
| def show(value: T) = | |
| s"Type: ${${Expr(Type.show[T])}}, Value: $value" | |
| } | |
| } | |
| // main.scala | |
| def printShow[T](value: T) = | |
| println(summon[Show[T]].show(value)) | |
| @main def main = | |
| printShow(42) | |
| printShow("Scala 3") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment