Does extending AnyVal on Implicit class offers any performance improvement. I heard that doing this way does not leads to Object creation, but how this happens.
1 Answers
you are talking about value classes I suppose. have a look at the documentation, is fairly simple and understandable: http://docs.scala-lang.org/overviews/core/value-classes.html
Let us know if you have any doubt anyway :)
Edit: trying to simplify the explanation provided on docs
value classes are replaced in compile time. if you have a
case class Name(value: String) extends AnyVal
at compile time, every instance of Name
will be replaced by the String
representation (I can't tell you in which phase, but I'm also curious now. I will try to find out and add the information here). Another question with some information may be this one I did a couple of weeks ago:
why scala value class#toString contains case class info?, which contains an answer linking to another article
Example:
if you have the following code:
def myFunction(name: Name) = {
"My name is: " + name.value
}
and you call it
myFunction(Name("Jonh"))
you can imagine it (imagine it, because I'm not sure if this is how it does or if it uses static objects and so on) being replaced by something like
def myFunction(name: String) = {
"My name is: " + name
}
myFunction("Jonh")

- 1
- 1

- 7,635
- 9
- 44
- 82
-
I did go through documentation but got into more confusion with the fact that value classes obviates need for Run time Object creation. I searched over web for answers but couldn't find any satisfactory one. Can you please help me with understanding as how value classes obviated Run time object creation and how the wrappers are represented by there values instead during the process – jack Dec 24 '16 at 12:17