Skip to content

Instantly share code, notes, and snippets.

@Krasnyanskiy
Forked from vkostyukov/views.scala
Created December 18, 2015 13:56
Show Gist options
  • Save Krasnyanskiy/427e7ef2c481bd2faf86 to your computer and use it in GitHub Desktop.
Save Krasnyanskiy/427e7ef2c481bd2faf86 to your computer and use it in GitHub Desktop.
Fancy implicit views
trait %>[From, To] {
def apply(x: From): To
}
def view[From, To](f: From => To): From %> To =
new %>[From, To] {
def apply(x: From): To = f(x)
}
implicit i: Int %> String = view(_.toString)
def foo[A](a: A)(implicit ev: A %> String): String = ev(a)
foo(Some(10)) // does not compile
foo(10) // compiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment