Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created November 2, 2023 10:00
Show Gist options
  • Save arturaz/73d42598df59eb680034aae2043244df to your computer and use it in GitHub Desktop.
Save arturaz/73d42598df59eb680034aae2043244df to your computer and use it in GitHub Desktop.
Implementation of an echo macro in Scala 3
extension [A](inline value: A) {
/**
* Echoes the value of the given expression.
*
* Example:
* {{{
* val foo = "Hi"
* foo.echo // "foo=Hi"
* }}}
*/
inline def echo: String = ${ echoImpl('value) }
}
object Consts {
def foo = "Hi"
}
@main
def main = {
println(Consts.foo.echo)
}
import scala.quoted.*
def echoImpl[A : Type](value: Expr[A])(using Quotes): Expr[String] = {
val nameExpr = Expr(value.show)
'{ s"${$nameExpr}=${$value}" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment