What are Scala's inline and transparent inline features in Scala 3?
Answer
Inline in Scala 3 is a compile-time feature that ensures code is inlined at call sites, enabling compile-time computations and eliminating abstraction overhead. inline def square(x: Int): Int = x * x — the call square(5) becomes 5 * 5 at compile time with no function call. inline if/match: branches are resolved at compile time: inline def log(msg: String): Unit = inline if debug then println(msg) — when debug is false at compile time, the println is completely eliminated. Transparent inline: allows the return type to be refined to the actual computed type: transparent inline def choose(b: Boolean): Int | String = inline if b then 42 else "hello" — the compiler knows the actual return type based on the argument. Inline parameters using inline: inline def callWith(inline body: => Unit). Inline replaces most macro use cases with a simpler, safer model. The Scala 3 standard library uses inline extensively for specialized operations and assertions that have zero runtime overhead.