Guest User

Untitled

a guest
Jul 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. import scala.language.higherKinds
  2. trait Foo[A] {
  3. type Out[_]
  4. def apply[T](t: T): Out[T]
  5. }
  6.  
  7. object Foo {
  8.  
  9. type Aux[A, Out0[_]] = Foo[A] { type Out[A] = Out0[A] }
  10.  
  11. implicit val fooInt: Aux[Int, List] = new Foo[Int] {
  12. type Out[A] = List[A]
  13. def apply[T](t: T): List[T] = List(t)
  14. }
  15.  
  16. implicit val fooString: Aux[String, Option] = new Foo[String] {
  17. type Out[A] = Option[A]
  18. def apply[T](t: T): Option[T] = Option(t)
  19. }
  20.  
  21. }
  22.  
  23. def works[A, T](a: A, t: T)(implicit
  24. foo: Foo[A]
  25. ): foo.Out[T] = foo(t)
  26.  
  27. def fails[A, FooOut[_], T](a: A, t: T)(implicit
  28. foo: Foo.Aux[A, FooOut]
  29. ): FooOut[T] = foo(t)
  30.  
  31. works(10, true)
  32. // res0: Foo.fooInt.Out[Boolean] = List(true)
  33.  
  34. fails(10, true)
  35. // error: could not find implicit value for parameter foo: Foo.Aux[Int,FooOut]
Add Comment
Please, Sign In to add comment