Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Created September 2, 2016 19:37
Show Gist options
  • Select an option

  • Save kevinmeredith/7f8bec9efda7b08090fb1f3ddf5f9967 to your computer and use it in GitHub Desktop.

Select an option

Save kevinmeredith/7f8bec9efda7b08090fb1f3ddf5f9967 to your computer and use it in GitHub Desktop.
dependent types with implicits
scala> sealed trait A { type T }
defined trait A
scala> case class AImpl(x: Int) extends A { override type T = Int }
defined class AImpl
scala> case class AImpl2(x: String) extends A { override type T = String }
defined class AImpl2
scala> def f(x: A)(implicit ev: x.T): x.T = ev
f: (x: A)(implicit ev: x.T)x.T
scala> implicit val int: Int = 42
int: Int = 42
scala> implicit val str: String = "foobar"
str: String = foobar
scala> f( AImpl(1) )
res0: Int = 42
scala> f( AImpl2("f") )
res1: String = foobar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment