In this blog , I will share my working knowledge on Value Classes, Universal Traits and Extension methods of Scala.

Let’s start with the Value Classes of Scala. In this section we will talk about Value Classes Introduction, Use Case and it’s limitations.

Value classes have been around in Scala for a long time internally, and we have used them already many times because all Number’s in Scala use this compiler trick to avoid boxing and unboxing numeric values from int to scala.Int etc.

As a quick reminder, let’s recall that Array[Int] is an actual JVM int[] which has tons of performance implications, but in one word — arrays of numbers are fast, arrays of references not as much. In more specific technical terms Value classes are a new mechanism in Scala to avoid allocating runtime objects. This is accomplished through the definition of new AnyVal subclasses. They were proposed in SIP-15. Ok, since we now know the compiler has fancy tricks to avoid boxing ints into Ints when it doesn’t have to. Let’s see how this feature is exposed for us, end users since Scala 2.10.x. The feature is called “value classes”, is fairly simple to apply to your existing classes. Using them is as simple as adding extends AnyVal to your class and following a few rules listed bellow.