Home

Awesome

Enum

Yet another enumeration toolbox for Scala, powered by shapeless.

Artifacts

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

License

This content is released under the MIT License.