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.Float, scala.Long, scala.Int, scala.Char, scala.Short, and scala.Byte are the numeric value types.
scala.Unit and scala.Boolean are the non-numeric value types.
Other groupings:
-
The subrange types are scala.Byte, scala.Short, and scala.Char.
-
The integer types include the subrange types as well as scala.Int and scala.Long.
-
The floating point types are scala.Float and scala.Double.
Prior to Scala 2.10, AnyVal
was a sealed trait. Beginning with Scala 2.10, however, it is possible to define a subclass of AnyVal
called a user-defined value class which is treated specially by the compiler. Properly-defined user value classes provide a way to improve performance on user-defined types by avoiding object allocation at runtime, and by replacing virtual method invocations with static method invocations.
User-defined value classes which avoid object allocation...
-
must have a single
val
parameter that is the underlying runtime representation. -
can define
def
s, but noval
s,var
s, or nestedtraits
s,class
es orobject
s. -
typically extend no other trait apart from
AnyVal
. -
cannot be used in type tests or pattern matching.
-
may not override
equals
orhashCode
methods.
A minimal example:
class Wrapper(val underlying: Int) extends AnyVal {
def foo: Wrapper = new Wrapper(underlying * 19)
}
It's important to note that user-defined value classes are limited, and in some circumstances, still must allocate a value class instance at runtime. These limitations and circumstances are explained in greater detail in the Value Classes and Universal Traits.
Attributes
- Graph
-
- Supertypes
- Known subtypes
-
class Deferrer[A]class Deferrer[A]class UnwrapOpclass ArrayOps[A]class IterableOnceExtensionMethods[A]class SizeCompareOpsclass UnapplySeqWrapper[A]class Shapeclass StringOpsclass DoubleMultclass DurationDoubleclass DurationIntclass DurationLongclass IntMultclass LongMultclass JavaDurationOpsclass ScalaDurationOpsclass RichBinaryOperatorAsFunction2[T]class RichConsumerAsFunction1[T]class RichDoubleFunctionAsFunction1[R]class RichFunction0AsSupplier[T]class RichFunction1AsConsumer[T]class RichFunction1AsDoubleFunction[R]class RichFunction1AsIntFunction[R]class RichFunction1AsLongFunction[R]class RichFunction1AsPredicate[T]class RichFunction1AsToDoubleFunction[T]class RichFunction1AsToIntFunction[T]class RichFunction1AsToLongFunction[T]class RichFunction1AsUnaryOperator[T]class RichFunction2AsBinaryOperator[T]class RichFunction2AsObjDoubleConsumer[T]class RichFunction2AsObjIntConsumer[T]class RichFunction2AsObjLongConsumer[T]class RichIntFunctionAsFunction1[R]class RichLongFunctionAsFunction1[R]class RichObjDoubleConsumerAsFunction2[T]class RichObjIntConsumerAsFunction2[T]class RichObjLongConsumerAsFunction2[T]class RichPredicateAsFunction1[T]class RichSupplierAsFunction0[T]class RichToDoubleFunctionAsFunction1[T]class RichToIntFunctionAsFunction1[T]class RichToLongFunctionAsFunction1[T]class RichUnaryOperatorAsFunction1[T]class CompletionStageOps[T]class FutureOps[T]class RichOption[A]class RichOptional[A]class RichOptionalDoubleclass RichOptionalIntclass RichOptionalLongclass RichBooleanclass RichByteclass RichCharclass RichDoubleclass RichFloatclass RichIntclass RichLongclass RichShortclass ChainingOps[A]class MergeableEither[A]class UnapplySeqWrapper[T]class Booleanclass Byteclass Charclass Doubleclass Floatclass Intclass Longclass ArrowAssoc[A]class Ensuring[A]class StringFormat[A]class any2stringadd[A]class Shortclass Unitclass ValueOf[T]