This doc page is specific to Scala 3, and may cover new concepts not available in Scala 2. Unless otherwise stated, all the code examples in this page assume you are using Scala 3.
Operations in scala.compiletime are metaprogramming operations that can be used within an inline
method.
These operation do cover some common use cases of macros without you needing to define a macro.
Reporting
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
If error
is called outside an inline method, the error will be emitted when compiling that call.
If the 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 codeOf
method.
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
Summoning
There are two ways to summon values in inline methods, the first is with a using
parameter and the second is with one of summonInline
, summonAll
or summonFrom
.
using
will summon the value at call site before inlining as if the method was not inline
.
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.
Values
constValue
,constValueOpt
andconstValueTuple
S
Coming soon
Testing
testing.typeChecks
andtesting.typeCheckErrors
Assertions
byName
Coming soon
Inline Matching
erasedValue
Coming soon
Ops (scala.compiletime.ops)
Coming soon