Skip to content

Commit

Permalink
Use lambda for SAM type class
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry committed Jun 23, 2019
1 parent 5b58dd7 commit f82add3
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,44 +123,42 @@ object cats {
}
}

implicit def catsTryFinally[F[_], A, B](implicit monadError: MonadThrowable[F]): TryFinally[A, F[B], F[A], F[Unit]] =
new TryFinally[A, F[B], F[A], F[Unit]] {
def tryFinally(block: F[A] !! A, finalizer: F[Unit] !! Unit, outerSuccessHandler: A => F[B]): F[B] = {
@inline
def injectFinalizer[A](f: Unit => F[A]): F[A] = {
monadError.flatMap(catchNativeException(finalizer))(f)
implicit def catsTryFinally[F[_], A, B](
implicit monadError: MonadThrowable[F]): TryFinally[A, F[B], F[A], F[Unit]] = {
(block: F[A] !! A, finalizer: F[Unit] !! Unit, outerSuccessHandler: A => F[B]) =>
@inline
def injectFinalizer[A](f: Unit => F[A]): F[A] = {
monadError.flatMap(catchNativeException(finalizer))(f)
}

monadError.flatMap(monadError.handleErrorWith(catchNativeException(block)) { e: Throwable =>
injectFinalizer { _: Unit =>
monadError.raiseError(e)
}
monadError.flatMap(monadError.handleErrorWith(catchNativeException(block)) { e: Throwable =>
injectFinalizer { _: Unit =>
monadError.raiseError(e)
}
}) { a =>
injectFinalizer { _: Unit =>
outerSuccessHandler(a)
}
}) { a =>
injectFinalizer { _: Unit =>
outerSuccessHandler(a)
}
}
}
}

implicit def catsTryCatch[F[_], A, B](implicit monadError: MonadThrowable[F]): TryCatch[A, F[B], F[A]] =
new TryCatch[A, F[B], F[A]] {
def tryCatch(block: F[A] !! A, catcher: Catcher[F[A] !! A], outerSuccessHandler: A => F[B]): F[B] = {
def errorHandler(e: Throwable): F[A] = {
(try {
catcher.lift(e)
} catch {
case NonFatal(extractorException) =>
return monadError.raiseError(extractorException)
}) match {
case None =>
monadError.raiseError(e)
case Some(recovered) =>
catchNativeException(recovered)
}
implicit def catsTryCatch[F[_], A, B](implicit monadError: MonadThrowable[F]): TryCatch[A, F[B], F[A]] = {
(block: F[A] !! A, catcher: Catcher[F[A] !! A], outerSuccessHandler: A => F[B]) =>
def errorHandler(e: Throwable): F[A] = {
(try {
catcher.lift(e)
} catch {
case NonFatal(extractorException) =>
return monadError.raiseError(extractorException)
}) match {
case None =>
monadError.raiseError(e)
case Some(recovered) =>
catchNativeException(recovered)
}
monadError.flatMap(monadError.handleErrorWith(catchNativeException(block))(errorHandler))(outerSuccessHandler)
}
}
monadError.flatMap(monadError.handleErrorWith(catchNativeException(block))(errorHandler))(outerSuccessHandler)
}

private[dsl] def catsCatchDsl[F[_], A, B](implicit monadError: MonadThrowable[F]): CatchDsl[F[A], F[B], A] = {
(block: F[A] !! A, catcher: Catcher[F[A] !! A], handler: A => F[B]) =>
Expand Down

0 comments on commit f82add3

Please sign in to comment.