From b5d7e0eaac2875405ce48c65906a03caacd9c2a6 Mon Sep 17 00:00:00 2001 From: Yang Bo Date: Sun, 23 Jun 2019 00:21:54 -0700 Subject: [PATCH] Use lambda for SAM type class --- .../com/thoughtworks/dsl/domains/cats.scala | 62 +++++++++---------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala b/domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala index 270f8096..7c3c6761 100644 --- a/domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala +++ b/domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala @@ -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]) =>