Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are AnyVal.types useful for?

Tags:

types

scala

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?

like image 217
agilesteel Avatar asked Sep 17 '11 22:09

agilesteel


People also ask

What is AnyVal?

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.

What is the value of class?

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.

What is type in Scala?

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.

What is a value class in Scala?

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.


1 Answers

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
like image 91
Kipton Barros Avatar answered Sep 23 '22 17:09

Kipton Barros