Awesome
Enum
Yet another enumeration toolbox for Scala, powered by shapeless.
Artifacts
- finds the values and labels of an enumeration and a mapping to go from a value to its label and vice versa.
- finds the set of values of an enumeration ;
- finds the set of value names of an enumeration ;
The artifacts are built for Scala 2.11, 2.12, 2.13 and Scala.js 0.6 and 1.x.
Usage
Just define your enumeration as a sealed trait (or a sealed abstract class) extended by case objects.
For instance:
sealed trait Foo
object Foo {
case object Bar extends Foo
case object Baz extends Foo
}
enum
Use Enum.derived[A]
to derive an Enum[A]
value that provides several useful methods:
import enum.Enum
val enum: Enum[Foo] = Enum.derived[Foo]
enum.values == Set(Foo.Bar, Foo.Baz)
enum.labels == Set("Bar", "Baz")
enum.encode(Foo.Bar) == "Bar"
enum.decode("Baz") == Right(Foo.Baz)
enum.decode("invalid") == Left(DecodingFailure[Foo](Set("Bar", "Baz")))
enum.decodeOpt("Baz") == Some(Foo.Baz)
See the API documentation for more details.
enum-values
Use Values.derived[A]
to automatically gather all the A
enumeration values as a Set[A]
:
import enum.Values
val fooValues: Values[Foo] = Values.derived[Foo]
fooValues.values == Set(Foo.Bar, Foo.Baz)
See the API documentation for more details.
enum-labels
Use Labels.derived[A]
to automatically gather all the A
enumeration labels as a Set[String]
:
import enum.Labels
val fooLabels: Labels[Foo] = Labels.derived[Foo]
fooLabels.labels == Set("Bar", "Baz")
See the API documentation for more details.
Changelog
- 3.1
- Scala 2.12 and 2.13 support
- 3.0
- Breaking change: the companion object’s
apply
method that was used to derive enumerations has been renamed toderived
. Theapply
method still exists but it now returns the implicitly available instance (your old code may still compile with the new version but might fail at runtime). - Remove package
julienrf
- Breaking change: the companion object’s
- 2.2
- Update shapeless to 2.3.0 and Scala.js to 0.6.7
- 2.1
- Lessen derivation implicit priority
- 2.0
- Added support for implicit definitions in enumerations companion objects
- 1.1
- Added
Enum[A]
- Added
- 1.0
- First release
License
This content is released under the MIT License.