Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Created January 28, 2013 00:25
Show Gist options
  • Save travisbrown/4651653 to your computer and use it in GitHub Desktop.
Save travisbrown/4651653 to your computer and use it in GitHub Desktop.
sealed trait Item
case class Marble() extends Item
case class Book() extends Item
sealed trait Container[Item]
case object Pouch extends Container[Marble]
case object Shelf extends Container[Book]
trait ContainerFor[I <: Item] { def apply(): Container[I] }
case class in[I <: Item](apply: Container[I]) extends ContainerFor[I]
def where[I <: Item](implicit mapping: ContainerFor[I]) = mapping()
implicit object putMarble extends in(Pouch)
implicit object putBook extends in(Shelf)
/* And then:
scala> where[Marble]
res0: Container[Marble] = Pouch
scala> where[Book]
res1: Container[Book] = Shelf
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment