Created
August 30, 2020 17:42
-
-
Save umbreak/9910e12f5d1edbb3945d3843b6f9dc72 to your computer and use it in GitHub Desktop.
Have an enum bind to a return type of a class hierarchy
This file contains 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
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