I've accidentally written this line of code:
scala> val f = Int
f: Int.type = object scala.Int
Seems to work for every subtype of AnyVal
except for AnyVal
itself.
Is there anything I can use f
for or is it just an implementation detail of Scala leaking out?
AnyVal is the root class of all value types, which describe values not implemented as objects in the underlying host system. Value classes are specified in Scala Language Specification, section 12.2. The standard implementation includes nine AnyVal subtypes: scala. Double, scala.
Value classes are a new mechanism which help to avoid allocating run time objects. AnyVal define value classes. Value classes are predefined, they coincide to the primitive kind of Java-like languages. There are nine predefined value types : Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean.
Scala is a statically typed programming language. This means the compiler determines the type of a variable at compile time. Type declaration is a Scala feature that enables us to declare our own types.
First proposed in SIP-15 and introduced in Scala 2.10. 0, value classes are a mechanism in Scala to avoid allocating runtime objects. This is accomplished through the definition of new AnyVal subclasses. The following shows a very minimal value class definition: class Wrapper(val underlying: Int) extends AnyVal.
The Int
companion object is documented here. It doesn't provide a lot, but here are a couple things:
scala> val f = Int
f: Int.type = object scala.Int
scala> f.MaxValue
res1: Int = 2147483647
scala> f.box(2)
res2: java.lang.Integer = 2
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