DaoWen
DaoWen

Reputation: 33019

What is the purpose of AnyVal?

I can't think of any situation where the type AnyVal would be useful, especially with the addition of the Numeric type for abstracting over Int, Long, etc. Are there any actual use cases for AnyVal, or is it just an artifact that makes the type hierarchy a bit prettier?


Just to clarify, I know what AnyVal is, I just can't think of any time that I would actually need it in Scala. When would I ever need a type that encompassed Int, Character and Double? It seems like it's just there to make the type hierarchy prettier (i.e. it looks nicer to have AnyVal and AnyRef as siblings rather than having Int, Character, etc. inherit directly from Any).

Upvotes: 6

Views: 7957

Answers (2)

som-snytt
som-snytt

Reputation: 39577

Let's go to the videotape, er, the spec 12.2:

Value classes are classes whose instances are not represented as objects by the underlying host system. All value classes inherit from class AnyVal.

So, maybe the question is, if everything is an object, why do I care if something is not represented i.e. implemented as an object? That's the implementation in implementation detail.

But let's not pretend, of course you care. Do you never specialize?

The spec goes on:

Scala implementations need to provide the value classes Unit, Boolean, Double, Float, Long, Int, Char, Short, and Byte (but are free to provide others as well).

Therefore a test for AnyVal is meaningful, over and above an enumeration of the required value classes.

That said, you must accept @drexin's answer because if you're not using value classes for extension methods, then you're not really living. (In the sense of, living it up.)

Motivation from the SIP:

...classes in Scala that can get completely inlined, so operations on these classes have zero overhead compared to external methods. Some use cases for inlined classes are:

  1. Inlined implicit wrappers. Methods on those wrappers would be translated to extension methods.
  2. New numeric classes, such as unsigned ints. There would no longer need to be a boxing overhead for such classes. So this is similar to value classes in .NET.
  3. Classes representing units of measure. Again, no boxing overhead would be incurred for these classes.

You can mark the extension method itself as @inline and everything is inlined: no object wrapper and your little method is inlined.

I use this feature every day. Yesterday I hit a bug in it. The bug is already fixed. What that says is that it's such a cool feature, the Scala folks will take time out from Coursera to quash a little bug in it.

That reminds me, I forgot to ask, this isn't a Coursera quiz question, is it?

Upvotes: 9

drexin
drexin

Reputation: 24413

As om-nom-nom already said, AnyVal is the common super type of all primitives in scala. In scala 2.10 however, there will be a new feature called value classes. Value classes are classes, that can be inlined, with this you can for example reduce the overhead of the extend my library pattern, because there will be no instances of the wrapper classes, that include these methods, instead they will be called statically. You can read everything about value classes in the SIP-15.

Upvotes: 11

Related Questions