Last active
May 25, 2024 10:18
-
-
Save dacr/1ad8a7a301aaea43fd4061586367d80c to your computer and use it in GitHub Desktop.
scala3 feature examples - macros - inline matches / published by https://github.com/dacr/code-examples-manager #d1f6d051-2dd9-4fbb-bf5a-dc855cd17c47/c6f7d05e5d86fea17b73adc7fe43167eb8ff45e2
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 matches | |
// 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 : d1f6d051-2dd9-4fbb-bf5a-dc855cd17c47 | |
// 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/inline.html | |
//> using scala "3.4.2" | |
import scala.compiletime.error | |
inline def half(x: Any): Any = { | |
inline x match | |
case x: Int => x / 2 | |
case x: String => x.substring(0, x.length / 2) | |
case _ => error("Unsupported input data type") // error potentially reported by the compiler | |
} | |
@main def go():Unit = { | |
println(half(42)) | |
println(half("The Answer to the Ultimate Question of Life, The Universe, and Everything")) | |
println(half('x')) // won't compile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment