Last active
February 3, 2026 20:23
-
-
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/88dcba8d60fc8295bb5c8759a824873df4764462
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
| // summary : scala3 feature examples - macros - programs as values | |
| // keywords : scala3, tutorial, macros, inline, meta-programming, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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.2" | |
| 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