Last active
February 3, 2026 20:25
-
-
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/6690c6de02c264e23eb3ef1c1f2d1f0140cc40f6
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 License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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