Last active
May 25, 2017 22:03
-
-
Save frgomes/da098c2f10a08440f5de to your computer and use it in GitHub Desktop.
Scala - Enumerations made easy... well... more or less
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// THIS IS Scala 2.11 | |
// see: https://github.com/d6y/enumeration-examples | |
object Status { | |
sealed abstract class enum(val id: Int, val name: String) extends Ordered[enum] { | |
def compare(that: enum) = this.id - that.id | |
} | |
case object Starting extends enum(0, "Starting") | |
case object Running extends enum(1, "Running") | |
case object Finished extends enum(2, "Finished") | |
val values = Seq(Starting, Running, Finished) | |
} | |
//------------------------------------------------------------------------------------------- | |
// THIS IS SCALA 2.13 | |
// see: https://github.com/scala/scala/pull/5352 | |
@enum | |
sealed trait Toggle | |
case object ON extends Toggle | |
case object OFF extends Toggle | |
//------------------------------------------------------------------------------------------- | |
// THIS IS enum using macros (quasiquotes) | |
// see: https://github.com/frgomes/poc-scalameta/blob/master/macros/src/main/scala/enum.scala | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't like the Status.enum bit. I suggest instead of the abstract class being an inner class of the object. Make it an actual class of the object. I'd also make a trait Enum and extend it but maybe that's just me