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
).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With