Last active
February 3, 2026 20:24
-
-
Save dacr/8f4682909eb44b7f6bfa4bafd596d60f to your computer and use it in GitHub Desktop.
scala3 feature examples - macros - inline / published by https://github.com/dacr/code-examples-manager #7919a7e2-aafd-4f7f-9702-acd157ef9bfc/a2dfe623f2be364cad057fb2f47797d6e6c6bab7
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 - inline | |
| // 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 : 7919a7e2-aafd-4f7f-9702-acd157ef9bfc | |
| // created-on : 2024-03-17T08:32:29+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/inline.html | |
| //> using scala "3.4.2" | |
| // fully inlined | |
| inline def perimeter(inline radius: Double): Double = { | |
| 2d * math.Pi * radius | |
| } | |
| // TAKE CARE inline parameters should be used only once | |
| // - to avoid side effect risks | |
| // - to avoid multiple evaluations : printPerimeter(longCompute()) | |
| inline def printPerimeter(inline radius: Double): Unit = { | |
| println(s"Perimeter(r=$radius)=${perimeter(radius)}") | |
| } | |
| @main def go() = { | |
| println(perimeter(42d)) // rewritten to : println(263.89378290154264) | |
| printPerimeter(42d) // rewritten to : Perimeter(r=42.0)=263.89378290154264 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment