Skip to content

Instantly share code, notes, and snippets.

@lancewalton
Last active October 15, 2017 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancewalton/7e5590815037ea55ce17cb7cf8d79fca to your computer and use it in GitHub Desktop.
Save lancewalton/7e5590815037ea55ce17cb7cf8d79fca to your computer and use it in GitHub Desktop.
// 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