Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Last active April 26, 2023 12:26
Show Gist options
  • Save gakuzzzz/4ad53153e28b3a6e0aa6 to your computer and use it in GitHub Desktop.
Save gakuzzzz/4ad53153e28b3a6e0aa6 to your computer and use it in GitHub Desktop.
Enum
trait EnumLike {
type Value
def value: Value
}
trait StringEnumLike extends EnumLike {
type Value = String
}
trait EnumCompanion[A <: EnumLike] {
def values: Seq[A]
def valueOf(value: A#Value): Option[A] = values.find(_.value == value)
}
abstract sealed class UserType(val value: String) extends StringEnumLike
object UserType extends EnumCompanion[UserType] {
case object Admin extends UserType("admin")
case object Regular extends UserType("regular")
case object Guest extends UserType("guest")
lazy val values = Seq(Admin, Regular, Guest)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment