Created
April 20, 2024 13:35
-
-
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/5fc690b8f59e434eac9b14f4c0f860e2837c49c7
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
// summary : scala3 feature examples - macros - inline | |
// 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 : 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.0" | |
// 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