Last active
October 15, 2017 21:52
-
-
Save lancewalton/7e5590815037ea55ce17cb7cf8d79fca to your computer and use it in GitHub Desktop.
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
// Is there a way to do this such that we don't end up with List[Thing[_]], | |
// but instead end up with something with the types preserved? | |
object Foo { | |
sealed trait Thing[T] | |
case class IntThing(i: Int) extends Thing[Int] | |
case class StringThing(s: String) extends Thing[String] | |
case class DoubleThing(d: Double) extends Thing[Double] | |
// The implementation here is only to get a compile | |
def differentThingsKnownOnlyAtRuntime(i: Int): Thing[_] = i match { | |
case 0 ⇒ IntThing(0) | |
case 1 ⇒ StringThing("Hello") | |
case 2 ⇒ DoubleThing(1.0) | |
} | |
val foo: List[Thing[_]] = (0 to 2).toList map differentThingsKnownOnlyAtRuntime | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment