Skip to content

Instantly share code, notes, and snippets.

@umbreak
Created August 30, 2020 17:42
Show Gist options
  • Save umbreak/9910e12f5d1edbb3945d3843b6f9dc72 to your computer and use it in GitHub Desktop.
Save umbreak/9910e12f5d1edbb3945d3843b6f9dc72 to your computer and use it in GitHub Desktop.
Have an enum bind to a return type of a class hierarchy
sealed trait Top
object Top {
final case class A(v: String) extends Top { def a: A = this }
final case class B(v: String) extends Top { def b: B = this }
def apply[V <: Top](select: TopEnum[V], value: String): V =
select match {
case SelectA => A(value)
case SelectB => B(value)
}
}
sealed trait TopEnum[V <: Top] extends Product with Serializable
object TopEnum {
final case object SelectA extends TopEnum[A]
final case object SelectB extends TopEnum[B]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment