I'm reading this article and there is this piece of code:
object ChildActor {
final val Name = "child-actor"
def apply(value: Int): Props = Props(new ChildActor(value))
}
and a note:
When defining constants final and starting with an upper case letter, the Scala compiler will inline them.
I don't get it. I know about method inlining, where a new stack frame is eliminated for the method call. But what does it mean for compiler to inline a constant, could you clarify?
Well, I am not familiar with scala per se, but the term "inline constant" means it will change the constant reference into a constant value, and directly hard-code the value of the constant to any reference at compile time. This eliminates the need for the additional memory space to keep the reference.
So, at compile time, the compiler changes the code such that
final val Name = "child-actor"
val otherName = Name
is treated as
final val Name = "child-actor"
val otherName = "child-actor"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With