Skip to content

Instantly share code, notes, and snippets.

@dacr
Created April 20, 2024 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/1ad8a7a301aaea43fd4061586367d80c to your computer and use it in GitHub Desktop.
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/fb6236c9c1bc5fc308a437448faff2ea48bcfd80
// 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.0"
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