Git Product home page Git Product logo

enum's Introduction

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.10 and 2.11 and Scala.js 0.6.

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.0
    • Breaking change: the companion object’s apply method that was used to derive enumerations has been renamed to derived. The apply 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
  • 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]
  • 1.0
    • First release

License

This content is released under the MIT License.

enum's People

Contributors

adelbertc avatar cchantep avatar danielskyfiitesting avatar julienrf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.