Created
September 2, 2016 19:37
-
-
Save kevinmeredith/7f8bec9efda7b08090fb1f3ddf5f9967 to your computer and use it in GitHub Desktop.
dependent types with implicits
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
| 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