- Notifications
You must be signed in to change notification settings - Fork2
One enum type class to rule them all
License
NotificationsYou must be signed in to change notification settings
scalalandio/enumz
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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 implementationyou pick, but you have no control overwhatother people use. So if you had to use APIs using many differentimplementations how would you handle common code?
With a type class.
Add to your sbt (2.12, 2.13 and 3.3+ 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 interfacefor all of them.
publicenumTestJavaEnum {A,B,C}
objectTestEnumerationextendsEnumeration {typeTestEnumeration=ValuevalA, B, C=Value}
sealedtraitTestSumTypeextendsProductwithSerializableobjectTestSumType {caseobjectAextendsTestSumTypecaseobjectBextendsTestSumTypecaseobjectCextendsTestSumType}
importenumeratum.{Enum=>EnumeratumEnum,_}sealedtraitTestEnumeratumextendsEnumEntryobjectTestEnumeratumextendsEnumeratumEnum[TestEnumeratum] {valvalues= findValuescaseobjectAextendsTestEnumeratumcaseobjectBextendsTestEnumeratumcaseobjectCextendsTestEnumeratum}
importio.scalaland.enumz.EnumEnum[TestJavaEnum].valuesEnum[TestEnumeration.TestEnumeration].valuesEnum[TestSumType].valuesEnum[TestEnumeratum].values
You can also test it with ammonite like:
import$ivy.`io.scalaland::enumz:1.0.0`,io.scalaland.enumz.Enum{sealedtraitTestSumTypeextendsProductwithSerializableobjectTestSumType {caseobjectAextendsTestSumTypecaseobjectBextendsTestSumTypecaseobjectCextendsTestSumType}}Enum[TestSumType].values
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)// 0Enum[TestSumType].withIndexOption(0)// Some(TestSumType.A)Enum[TestSumType].withIndexOption(-1)// NoneEnum[TestSumType].withIndex(0)// TestSumType.AEnum[TestSumType].withIndex(-1)// throws!Enum[TestSumType].withNameOption("A")// Some(TestSumType.A)Enum[TestSumType].withNameOption("")// NoneEnum[TestSumType].withName("A")// TestSumType.AEnum[TestSumType].withName("")// throws!Enum[TestSumType].withNameInsensitiveOption("a")// Some(TestSumType.A)Enum[TestSumType].withNameInsensitiveOption("")// NoneEnum[TestSumType].withNameInsensitive("a")// TestSumType.AEnum[TestSumType].withNameInsensitive("")// throws!Enum[TestSumType].`A`// TestSumType.A
About
One enum type class to rule them all
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published