Last active
May 25, 2024 10:19
-
-
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/3ae11f83aec4e2fbb6dc15f772125a9241ee9da7
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 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.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