enumz

Common type class interface for various enums implementations

License

License

Categories

Categories

Scala Languages
GroupId

GroupId

io.scalaland
ArtifactId

ArtifactId

enumz_2.11
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

enumz
Common type class interface for various enums implementations
Project Organization

Project Organization

io.scalaland
Source Code Management

Source Code Management

https://github.com/scalalandio/enumz

Download enumz_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/io.scalaland/enumz_2.11/ -->
<dependency>
    <groupId>io.scalaland</groupId>
    <artifactId>enumz_2.11</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.scalaland/enumz_2.11/
implementation 'io.scalaland:enumz_2.11:1.0.0'
// https://jarcasting.com/artifacts/io.scalaland/enumz_2.11/
implementation ("io.scalaland:enumz_2.11:1.0.0")
'io.scalaland:enumz_2.11:jar:1.0.0'
<dependency org="io.scalaland" name="enumz_2.11" rev="1.0.0">
  <artifact name="enumz_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.scalaland', module='enumz_2.11', version='1.0.0')
)
libraryDependencies += "io.scalaland" % "enumz_2.11" % "1.0.0"
[io.scalaland/enumz_2.11 "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
com.beachape : enumeratum_2.11 jar 1.5.13

test (3)

Group / Artifact Type Version
org.specs2 : specs2-core_2.11 jar 4.6.0
org.specs2 : specs2-scalacheck_2.11 jar 4.6.0
org.specs2 : specs2-matcher-extra_2.11 jar 4.6.0

Project Modules

There are no modules declared in this project.

enumz

Build Status Maven Central Scala.js License

One enum type class to rule them all.

In Scala you might meet many different implementations of enums:

  • build-in scala.Enumeration,
  • sum-type based sealed hierarchies,
  • enumeratum as the previous one on steroids,
  • Java's enum type which use static methods and has no companion object.

You are in control of what implementation you pick, but you have no control over what other people use. So if you had to use APIs using many different implementations how would you handle common code?

With a type class.

Usage

Add to your sbt (2.11, 2.12 and 2.13 supported)

libraryDependencies += "io.scalaland" %% "enumz" % enumzVersion // see Maven badge

or, if you use Scala.js

libraryDependencies += "io.scalaland" %%% "enumz" % enumzVersion // see Maven badge

From now on you can define enums whatever you want and use one common interface for all of them.

public enum TestJavaEnum {
    A, B, C
}
object TestEnumeration extends Enumeration {
  type TestEnumeration = Value
  val A, B, C = Value
}
sealed trait TestSumType extends Product with Serializable
object TestSumType {
  case object A extends TestSumType
  case object B extends TestSumType
  case object C extends TestSumType
}
import enumeratum.{Enum => EnumeratumEnum, _}

sealed trait TestEnumeratum extends EnumEntry
object TestEnumeratum extends EnumeratumEnum[TestEnumeratum] {
  val values = findValues
  case object A extends TestEnumeratum
  case object B extends TestEnumeratum
  case object C extends TestEnumeratum
}
import io.scalaland.enumz.Enum

Enum[TestJavaEnum].values
Enum[TestEnumeration.TestEnumeration].values
Enum[TestSumType].values
Enum[TestEnumeratum].values

You can also test it with ammonite like:

import $ivy.`io.scalaland::enumz:1.0.0`, io.scalaland.enumz.Enum

{
sealed trait TestSumType extends Product with Serializable
object TestSumType {
  case object A extends TestSumType
  case object B extends TestSumType
  case object C extends TestSumType
}
}

Enum[TestSumType].values

Methods

Enum[TestSumType].values // Vector(TestSumType.A, TestSumType.B, TestSumType.C)
Enum[TestSumType].indices // Map(TestSumType.A -> 0, TestSumType.B -> 1, TestSumType.C -> 2)

Enum[TestSumType].getName(TestSumType.A) // "A"
Enum[TestSumType].getIndex(TestSumType.A) // 0

Enum[TestSumType].withIndexOption(0) // Some(TestSumType.A)
Enum[TestSumType].withIndexOption(-1) // None

Enum[TestSumType].withIndex(0) // TestSumType.A
Enum[TestSumType].withIndex(-1) // throws!

Enum[TestSumType].withNameOption("A") // Some(TestSumType.A)
Enum[TestSumType].withNameOption("") // None

Enum[TestSumType].withName("A") // TestSumType.A
Enum[TestSumType].withName("") // throws!

Enum[TestSumType].withNameInsensitiveOption("a") // Some(TestSumType.A)
Enum[TestSumType].withNameInsensitiveOption("") // None

Enum[TestSumType].withNameInsensitive("a") // TestSumType.A
Enum[TestSumType].withNameInsensitive("") // throws!

Enum[TestSumType].`A` // TestSumType.A
io.scalaland

Scalaland.io

We have another fantastic day writing Scala

Versions

Version
1.0.0
0.1.2