Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/6af6ef8bcc227075f7e2bbd8e7eaf06d to your computer and use it in GitHub Desktop.
Save dacr/6af6ef8bcc227075f7e2bbd8e7eaf06d to your computer and use it in GitHub Desktop.
scala3 feature examples - macros - programs as values / published by https://github.com/dacr/code-examples-manager #9073c9cf-9998-4ee6-983f-fbe3acf48d94/f9d203c76b8fc226312cca9998cd86a560b40e8d
// summary : scala3 feature examples - macros - programs as values
// keywords : scala3, tutorial, macros, inline, meta-programming, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 9073c9cf-9998-4ee6-983f-fbe3acf48d94
// created-on : 2024-03-17T11:11:11+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// Inspired from https://docs.scala-lang.org/scala3/guides/macros/macros.html
//> using scala "3.4.0"
import scala.quoted.*
def inspectCode(x: Expr[Any])(using Quotes): Expr[Any] = {
println(x.show)
x
}
inline def inspect(inline x: Any): Any = ${ inspectCode('x) }
@main def go(): Unit = {
val x = 42
inspect(s"answer to the meaning of life is $x")
}
// TODO issue with scala-cli ?
// 11:31 $ scl scala3-feature-macro-5-programs-as-values.scala
// Compiling project (Scala 3.4.0, JVM (21))
// [error] ./scala3-feature-macro-5-programs-as-values.scala:26:3
// [error] Cannot call macro method inspectCode defined in the same source file
// [error] inspect(s"answer to the meaning of life is $x")
// [error] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Error compiling project (Scala 3.4.0, JVM (21))
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment