Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to inline a constant?

Tags:

scala

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?

like image 342
Alexander Arendar Avatar asked Jan 25 '18 00:01

Alexander Arendar


1 Answers

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"
like image 67
Yamuk Avatar answered Nov 17 '22 09:11

Yamuk