Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats._
- import cats.implicits._
- trait Service[R[_]] {
- type Result[T] = R[T]
- def computeValue(): Result[Int]
- }
- class ServiceImpl[R[_] : Monad : Foldable]
- (
- )
- extends Service[R] {
- protected val RM: Monad[R] = implicitly[Monad[R]]
- protected val RF: Foldable[R] = implicitly[Foldable[R]]
- def computeValue(): Result[Int] = {
- RM.pure {
- 1
- }
- }
- }
- object Test {
- type Res[R] = Id[R]
- def main(args: Array[String]): Unit = {
- {
- val service = new ServiceImpl[Id]()
- val result = 1 + service.computeValue() // Int
- println(result) // 2
- }
- {
- import scala.util._
- val service = new ServiceImpl[Try]()
- val result = service.computeValue().map(_ + 1) // Try[Int]
- println(result) // Success(2)
- }
- }
- }
- Test.main(Array.empty)
Add Comment
Please, Sign In to add comment