Created
November 2, 2023 10:00
-
-
Save arturaz/73d42598df59eb680034aae2043244df to your computer and use it in GitHub Desktop.
Implementation of an echo macro in Scala 3
This file contains 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
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) | |
} |
This file contains 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
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