Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/AnyVal

AnyVal

scala.AnyVal
abstract openclassAnyVal

AnyVal is the root class of allvalue 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 nineAnyVal subtypes:

scala.Double,scala.Float,scala.Long,scala.Int,scala.Char,scala.Short, andscala.Byte are thenumeric value types.

scala.Unit andscala.Boolean are thenon-numeric value types.

Other groupings:

Prior to Scala 2.10,AnyVal was a sealed trait. Beginning with Scala 2.10, however, it is possible to define a subclass ofAnyVal called auser-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 singleval parameter that is the underlying runtime representation.

  • can definedefs, but novals,vars, or nestedtraitss,classes orobjects.

  • typically extend no other trait apart fromAnyVal.

  • cannot be used in type tests or pattern matching.

  • may not overrideequals 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 theValue Classes and Universal Traits.

Attributes

Graph
Supertypes
traitMatchable
classAny
Known subtypes
classDeferrer[A]
classPartial[T,U]
classDeferrer[A]
classUnwrapOp
classArrayOps[A]
classSearchImpl[Repr,A]
classShape
classStringOps
classIntMult
classLongMult
classFutureOps[T]
classRichOption[A]
classRichOptional[A]
classRichByte
classRichChar
classRichFloat
classRichInt
classRichLong
classRichShort
classTuple2Zipped[El1,It1,El2,It2]
classOps[T1,T2]
classTuple3Zipped[El1,It1,El2,It2,El3,It3]
classOps[T1,T2,T3]
classChainingOps[A]
classBoolean
classByte
classChar
classDouble
classFloat
classUnliftOps[A,B]
classInt
classLong
classArrowAssoc[A]
classEnsuring[A]
classStringFormat[A]
classany2stringadd[A]
classShort
classUnit
classValueOf[T]
Show all
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp