Guest User

Untitled

a guest
Mar 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import cats._
  2. import cats.implicits._
  3.  
  4. trait Service[R[_]] {
  5. type Result[T] = R[T]
  6.  
  7. def computeValue(): Result[Int]
  8. }
  9.  
  10. class ServiceImpl[R[_] : Monad : Foldable]
  11. (
  12.  
  13. )
  14. extends Service[R] {
  15. protected val RM: Monad[R] = implicitly[Monad[R]]
  16. protected val RF: Foldable[R] = implicitly[Foldable[R]]
  17.  
  18. def computeValue(): Result[Int] = {
  19. RM.pure {
  20. 1
  21. }
  22. }
  23. }
  24.  
  25. object Test {
  26. type Res[R] = Id[R]
  27.  
  28. def main(args: Array[String]): Unit = {
  29. {
  30. val service = new ServiceImpl[Id]()
  31. val result = 1 + service.computeValue() // Int
  32. println(result) // 2
  33. }
  34.  
  35. {
  36. import scala.util._
  37. val service = new ServiceImpl[Try]()
  38.  
  39. val result = service.computeValue().map(_ + 1) // Try[Int]
  40. println(result) // Success(2)
  41. }
  42. }
  43. }
  44.  
  45. Test.main(Array.empty)
Add Comment
Please, Sign In to add comment