What is the purpose of AnyVal?

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


Just to clarify, I know that AnyVal , I just can’t think that I really need it in Scala. When will I need a type spanning Int , Character and Double ? It seems like he just wants to make the type hierarchy more beautiful (i.e., it is better to have AnyVal and AnyRef as siblings, and not have Int , Character , etc. Inherit directly from Any ).

+4
source share
2 answers

Go to the video, er, spec 12.2:

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

So, maybe the question is, if everything is an object, why does it bother me if something is not represented, for example, implemented as an object? This is described in detail in the implementation.

But don't let it pretend, of course, you don't care. Do you never specialize?

The spectrum continues:

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

Therefore, the AnyVal test makes sense, in addition to listing the required value classes.

However, you must accept @drexin's answer, because if you do not use value classes for extension methods, then you really do not live. (I mean, living it.)

Motivation from SIP:

... classes in Scala that can be fully nested, so operations with these classes have zero overhead compared to external methods. Some for individual classes:

  • Nested implicit wrappers. The methods of these wrappers will be translated into extension methods.
  • New number classes such as unsigned ints. Will no longer be for such classes should be overhead boxes. So it looks like a class in .NET.
  • Classes representing units of measure. Again, boxing overhead will be incurred for these classes.

You can mark the extension method itself as @inline, and everything is included: no object wrapper and your little method are built-in.

I use this feature every day. Yesterday I hit him. The error has already been fixed. What this says is that such a wonderful feature, Scala people will spend time on Coursera to undo a small mistake.

It reminds me, I forgot to ask, this is not a question from the Coursera survey, is it?

+7
source

As already mentioned, om-nom-nom, AnyVal is the common super-type of all primitives in scala. However, a new value classes function will appear in scala 2.10. Value classes are classes that can be embedded, and you can, for example, reduce the overhead of expanding my library template because there will be no instances of wrapper classes that include these methods, they will be called statically instead. You can read all about value classes in SIP-15 .

+9
source

Source: https://habr.com/ru/post/1436966/


All Articles