Why would you wrap a single value into an additional class?
One big use case is type safety. Let's say you have function that can multiply money, like so:
def multiply(factor: Int, amount: Int): Int = ???
The problem with this is that it would be very easy to confuse the two arguments and therefore call the function incorrectly. With values classes, you could create a Money
type and re-write the function like so:
case class Money(amount: Int) extends AnyVal
def multiply(factor: Int, amount: Money): Money = ???
Now with your special Money
type, the compiler will tell you if you try to pass arguments in the wrong order.
Were it not a value class, people may say that the added type safety is not worth the performance penalty in some cases. However with value classes, you have no runtime overhead (there are limitations though: http://docs.scala-lang.org/overviews/core/value-classes.html).
An alternative to achieve the same goal are unboxed (no runtime overhead) tagged types in scalaz: http://eed3si9n.com/learning-scalaz/Tagged+type.html
Note that for example haskell uses newtype
for the same idea: https://wiki.haskell.org/Newtype