Skip to content

Instantly share code, notes, and snippets.

@nagat01
Last active September 3, 2024 03:22
Show Gist options
  • Select an option

  • Save nagat01/b580994a8a2863633e54fa157effd6f9 to your computer and use it in GitHub Desktop.

Select an option

Save nagat01/b580994a8a2863633e54fa157effd6f9 to your computer and use it in GitHub Desktop.
// 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