Yet another enumeration toolbox for Scala, powered by shapeless.
Several artifacts are available:
enum-values: finds the set of values of an enumeration ;
enum-labels: finds the set of value names of an enumeration ;
enum: finds the values and labels of an enumeration and a mapping to go from a value to its label and vice versa.
The artifacts are built for Scala 2.11 and Scala.js 0.6.
Just define your enumeration as a sealed trait extended by case objects.
For instance:
sealed trait Foo
object Foo {
case object Bar extends Foo
case object Baz extends Foo
}Use Values[A] to automatically gather all the A enumeration values as a Set[A]:
import julienrf.enum.Values
val fooValues: Values[Foo] = Values[Foo]
fooValues.values == Set(Foo.Bar, Foo.Baz)Use Labels[A] to automatically gather all the A enumeration labels as a Set[String]:
import julienrf.enum.Labels
val fooLabels: Labels[Foo] = Labels[Foo]
fooLabels.labels == Set("Bar", "Baz")import julienrf.enum.{Enum, DecodingFailure}
val enum: Enum[Foo] = Enum[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)- 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
This content is released under the MIT License.