Advertisement
Guest User

Untitled

a guest
May 4th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. trait R[+A] { self =>
  2. type In
  3.  
  4. def apply(in: In): A
  5.  
  6. def map[B](f: A => B): R[B] = new R[B] {
  7. type In = self.In
  8. def apply(in: In) = f(self(in))
  9. }
  10.  
  11. def flatMap[B](f: A => R[B] { type In = self.In }): R[B] = new R[B] {
  12. type In = self.In
  13. def apply(in: In): B = f(self(in))(in)
  14. }
  15. }
  16.  
  17. def foo[I, A](a: A): R[A] = new R[A] {
  18. type In = I
  19. def apply(in: In): A = a
  20. }
  21.  
  22. //
  23. // compiles
  24. //
  25. val a: R[String] = foo[Int, String]("b").map(_ + "a")
  26.  
  27. // does not compile
  28. //
  29. // Error:(20, 53) type mismatch;
  30. // found : A$A58.this.AA[String]
  31. // required: A$A58.this.AA[?]{type R = A$A58.this.AA[String]#R}
  32. // foo[Int, String]("a").flatMap(x => foo[Int, String](x));}
  33. //
  34. val b: R[String] = a.flatMap(x => foo[Int, String](x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement