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/Enumeration

Enumeration

scala.Enumeration
abstractclassEnumeration(initial:Int) extendsSerializable

Defines a finite set of values specific to the enumeration. Typically these values enumerate all possible forms something can take and provide a lightweight alternative to case classes.

Each call to aValue method adds a new unique value to the enumeration. To be accessible, these values are usually defined asval members of the enumeration.

All values in an enumeration share a common, unique type defined as theValue type member of the enumeration (Value selected on the stable identifier path of the enumeration instance).

Values SHOULD NOT be added to an enumeration after its construction; doing so makes the enumeration thread-unsafe. If values are added to an enumeration from multiple threads (in a non-synchronized fashion) after construction, the behavior of the enumeration is undefined.

Value parameters

initial

The initial value from which to count the integers that identifies values at run-time.

Attributes

Example

// Define a new enumeration with a type alias and work with the full set of enumerated valuesobject WeekDay extends Enumeration { type WeekDay = Value val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value}import WeekDay._def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)WeekDay.values filter isWorkingDay foreach println// output:// Mon// Tue// Wed// Thu// Fri

// Example of adding attributes to an enumeration by extending the Enumeration.Val classobject Planet extends Enumeration { protected case class PlanetVal(mass: Double, radius: Double) extends super.Val {   def surfaceGravity: Double = Planet.G * mass / (radius * radius)   def surfaceWeight(otherMass: Double): Double = otherMass * surfaceGravity } import scala.language.implicitConversions implicit def valueToPlanetVal(x: Value): PlanetVal = x.asInstanceOf[PlanetVal] val G: Double = 6.67300E-11 val Mercury = PlanetVal(3.303e+23, 2.4397e6) val Venus   = PlanetVal(4.869e+24, 6.0518e6) val Earth   = PlanetVal(5.976e+24, 6.37814e6) val Mars    = PlanetVal(6.421e+23, 3.3972e6) val Jupiter = PlanetVal(1.9e+27, 7.1492e7) val Saturn  = PlanetVal(5.688e+26, 6.0268e7) val Uranus  = PlanetVal(8.686e+25, 2.5559e7) val Neptune = PlanetVal(1.024e+26, 2.4746e7)}println(Planet.values.filter(_.radius > 7.0e6))// output:// Planet.ValueSet(Jupiter, Saturn, Uranus, Neptune)
Source
Enumeration.scala
Graph
Supertypes
classObject
traitMatchable
classAny
Known subtypes
Self type

Members list

Type members

Classlikes

protectedclassVal(i:Int,name:String) extendsValue,Serializable

A class implementing thescala.Enumeration.Value type.

A class implementing thescala.Enumeration.Value type. This class can be overridden to change the enumeration's naming and integer identification behaviour.

Attributes

Source
Enumeration.scala
Supertypes
classEnumeration.this.Value
classObject
traitMatchable
classAny
Show all
abstractclassValue extendsOrdered[Value],Serializable

The type of the enumerated values.

The type of the enumerated values.

Attributes

Source
Enumeration.scala
Supertypes
classObject
traitMatchable
classAny
Show all
Known subtypes
classEnumeration.this.Val

An ordering by id for values of this set

An ordering by id for values of this set

Attributes

Source
Enumeration.scala
Supertypes
traitEquiv[Enumeration.this.Value]
classObject
traitMatchable
classAny
Show all
Self type

A class for sets of values.

A class for sets of values. Iterating through this set will yield values in increasing order of their ids.

Value parameters

nnIds

The set of ids of values (adjusted so that the lowest value does not fall below zero), organized as aBitSet.

Attributes

Companion
object
Source
Enumeration.scala
Supertypes
traitSet[Enumeration.this.Value]
traitSet[Enumeration.this.Value]
traitEquals
classObject
traitMatchable
classAny
Show all

A factory object for value sets

A factory object for value sets

Attributes

Companion
class
Source
Enumeration.scala
Supertypes
Self type

Value members

Constructors

defthis()

Attributes

Source
Enumeration.scala

Concrete methods

final protecteddefValue:Value

Creates a fresh value, part of this enumeration.

Creates a fresh value, part of this enumeration.

Attributes

Source
Enumeration.scala
final protecteddefValue(i:Int):Value

Creates a fresh value, part of this enumeration, identified by the integeri.

Creates a fresh value, part of this enumeration, identified by the integeri.

Value parameters

i

An integer that identifies this value at run-time. It must be unique amongst all values of the enumeration.

Attributes

Returns

Fresh value identified byi.

Source
Enumeration.scala
final protecteddefValue(name:String):Value

Creates a fresh value, part of this enumeration, calledname.

Creates a fresh value, part of this enumeration, calledname.

Value parameters

name

A human-readable name for that value.

Attributes

Returns

Fresh value calledname.

Source
Enumeration.scala
final protecteddefValue(i:Int,name:String):Value

Creates a fresh value, part of this enumeration, calledname and identified by the integeri.

Creates a fresh value, part of this enumeration, calledname and identified by the integeri.

Value parameters

i

An integer that identifies this value at run-time. It must be unique amongst all values of the enumeration.

name

A human-readable name for that value.

Attributes

Returns

Fresh value with the provided identifieri and namename.

Source
Enumeration.scala
finaldefapply(x:Int):Value

The value of this enumeration with given idx

The value of this enumeration with given idx

Attributes

Source
Enumeration.scala
finaldefmaxId:Int

The one higher than the highest integer amongst those used to identify values in this enumeration.

The one higher than the highest integer amongst those used to identify values in this enumeration.

Attributes

Source
Enumeration.scala
protecteddefreadResolve():AnyRef

Attributes

Source
Enumeration.scala
overridedeftoString:String

The name of this enumeration.

The name of this enumeration.

Attributes

Definition Classes
Source
Enumeration.scala

The values of this enumeration as a set.

The values of this enumeration as a set.

Attributes

Source
Enumeration.scala
finaldefwithName(s:String):Value

Return aValue from thisEnumeration whose name matches the arguments.

Return aValue from thisEnumeration whose name matches the arguments. The names are determined automatically via reflection.

Value parameters

s

anEnumeration name

Attributes

Returns

theValue of thisEnumeration if its name matchess

Throws

NoSuchElementExceptionif noValue with a matching name is in thisEnumeration

Source
Enumeration.scala

Concrete fields

protectedvarnextId:Int

The integer to use to identify the next created value.

The integer to use to identify the next created value.

Attributes

Source
Enumeration.scala
protectedvarnextName:Iterator[String]

The string to use to name the next created value.

The string to use to name the next created value.

Attributes

Source
Enumeration.scala
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp