This doc page is specific to Scala 3, and may cover new concepts not available in Scala 2.
Operations in scala.compiletime are metaprogramming operations that can be used within an
These operation do cover some common use cases of macros without you needing to define a macro.
It is possible to emit error messages when inlining code.
inline def doSomething(inline mode: Boolean): Unit = if mode then ... else if !mode then ... else error("Mode must be a known value") doSomething(true) doSomething(false) val bool: Boolean = ... doSomething(bool) // error: Mode must be a known value
error is called outside an inline method, the error will be emitted when compiling that call.
error is written inside an inline method, the error will be emitted only if, after inlining the call, it is not removed as part of a dead branch.
In the previous example, if the value of
mode were known at compile time, we would only keep one of the first two branches.
If we want to include part of the source code of the arguments in the error message, we can use the
inline def doSomething(inline mode: Boolean): Unit = if mode then ... else if !mode then ... else error("Mode must be a known value but got: " + codeOf(mode)) val bool: Boolean = ... doSomething(bool) // error: Mode must be a known value but got: bool
There are two ways to summon values in inline methods, the first is with a
using parameter and the second is with one of
using will summon the value at call site before inlining as if the method was not
On the other hand,
summonInline will summon after inlining if the call is not eliminated from a dead branch.
summonAll provides a way to summon multiple values at the same time from a tuple type.
summonFrom provides a way to try several implicit searches.