Wonpyo Park Wonpyo Park - 1 year ago 142
Scala Question

Scala value class, use cases

I know

value class
in scala inline the operation at compiler time.

maybe like this

case class A(i: Int) extends AnyVal {
def +(that: A) = A(this.i + that.i)
A(1) + A(2) // After compile it equals to 1 + 2

But It seems not a big deal to me.

It might enhance performance but,

this.i + that.i
does not seems that much slower than
i + i

Why we need
value class
in scala and any use cases???

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download