Skip to content

Commit

Permalink
Merge pull request #321 from Kevin-Lee/task/320-remove-traits
Browse files Browse the repository at this point in the history
Close #320 - Remove `Fx`, `FxCtor` and `CanCatch` `trait`s from `effectie-cats-effect`, `effectie-cats-effect3` and `effectie-monix`
  • Loading branch information
kevin-lee committed Nov 27, 2021
2 parents 7e8adb5 + f0c2753 commit 401adc1
Show file tree
Hide file tree
Showing 92 changed files with 631 additions and 539 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def projectCommonSettings(id: String, projectName: ProjectName, file: File): Pro
.settings(
name := prefixedProjectName(projectName.projectName),
scalacOptions ~= (_.filterNot(props.isScala3IncompatibleScalacOption)),
libraryDependencies ++= libs.hedgehogLibs.map(_ % Test),
libraryDependencies ++= libs.hedgehogLibs.map(_ % Test) ++ List("io.kevinlee" %% "extras-cats" % "0.2.0" % Test),
/* WartRemover and scalacOptions { */
// Compile / compile / wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value),
// Test / compile / wartremoverErrors ++= commonWarts((update / scalaBinaryVersion).value),
Expand Down
34 changes: 1 addition & 33 deletions cats-effect/src/main/scala-2/effectie/cats/CanCatch.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
package effectie.cats

import cats.Id
import cats.data.EitherT
import cats.effect.IO
import cats.syntax.all._

import scala.concurrent.{ExecutionContext, Future}

/** @author Kevin Lee
* @since 2020-06-07
*/
trait CanCatch[F[_]] extends effectie.CanCatch[F] {

override type XorT[A, B] = EitherT[F, A, B]

@inline override final protected def xorT[A, B](fab: F[Either[A, B]]): EitherT[F, A, B] = EitherT(fab)

@inline override final protected def xorT2FEither[A, B](efab: EitherT[F, A, B]): F[Either[A, B]] = efab.value

}

object CanCatch {
def apply[F[_]: CanCatch]: CanCatch[F] = implicitly[CanCatch[F]]
type CanCatch[F[_]] = effectie.CanCatch[F]

implicit object CanCatchIo extends CanCatch[IO] {

Expand All @@ -32,26 +20,6 @@ object CanCatch {

}

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
implicit def canCatchFuture(implicit EC: ExecutionContext): CanCatch[Future] =
new effectie.CanCatch.CanCatchFuture with CanCatch[Future] {

override val EC0: ExecutionContext = EC

override def catchNonFatalThrowable[A](fa: => Future[A]): Future[Either[Throwable, A]] =
fa.transform {
case scala.util.Success(a) =>
scala.util.Try[Either[Throwable, A]](Right(a))

case scala.util.Failure(scala.util.control.NonFatal(ex)) =>
scala.util.Try[Either[Throwable, A]](Left(ex))

case scala.util.Failure(ex) =>
throw ex
}(EC0)

}

implicit object CanCatchId extends CanCatch[Id] {

@inline override final def mapFa[A, B](fa: Id[A])(f: A => B): Id[B] = f(fa)
Expand Down
28 changes: 13 additions & 15 deletions cats-effect/src/main/scala-2/effectie/cats/Catching.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import cats.data.EitherT
*/
trait Catching {

import Catching._
import effectie.cats.Catching._

final def catchNonFatalThrowable[F[_]]: CurriedCanCatchThrowable[F] =
new CurriedCanCatchThrowable[F]
Expand All @@ -27,8 +27,6 @@ trait Catching {
def catchNonFatalEitherT[F[_]]: CurriedCanCatchEitherT1[F] =
new CurriedCanCatchEitherT1[F]

// def catchNonFatalEitherT[F[_], A, B](fab: => EitherT[F, A, B])(f: Throwable => A)(implicit CC: CanCatch[F]): EitherT[F, A, B] =
// CanCatch[F].catchNonFatalEitherT(fab)(f)
}

@SuppressWarnings(Array("org.wartremover.warts.DefaultArguments"))
Expand All @@ -37,8 +35,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchThrowable[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[B](fb: => F[B])(implicit CC: CanCatch[F]): F[Either[Throwable, B]] =
CanCatch[F].catchNonFatalThrowable[B](fb)
def apply[B](fb: => F[B])(implicit CC: effectie.CanCatch[F]): F[Either[Throwable, B]] =
effectie.CanCatch[F].catchNonFatalThrowable[B](fb)
}

private[Catching] final class CurriedCanCatch1[F[_]](
Expand All @@ -51,8 +49,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatch2[F[_], B](
private val fb: () => F[B]
) extends AnyVal {
def apply[A](f: Throwable => A)(implicit CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatal(fb())(f)
def apply[A](f: Throwable => A)(implicit CC: effectie.CanCatch[F]): F[Either[A, B]] =
effectie.CanCatch[F].catchNonFatal(fb())(f)
}

@SuppressWarnings(Array("org.wartremover.warts.DefaultArguments"))
Expand All @@ -66,8 +64,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchF2[F[_], B](
private val b: () => B
) extends AnyVal {
def apply[A](f: Throwable => A)(implicit EC: FxCtor[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatal(FxCtor[F].effectOf(b()))(f)
def apply[A](f: Throwable => A)(implicit EC: effectie.FxCtor[F], CC: effectie.CanCatch[F]): F[Either[A, B]] =
effectie.CanCatch[F].catchNonFatal(effectie.FxCtor[F].effectOf(b()))(f)
}

private[Catching] final class CurriedCanCatchEither1[F[_]](
Expand All @@ -80,8 +78,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchEither2[F[_], A, B](
private val fab: () => F[Either[A, B]]
) extends AnyVal {
def apply(f: Throwable => A)(implicit CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatalEither(fab())(f)
def apply(f: Throwable => A)(implicit CC: effectie.CanCatch[F]): F[Either[A, B]] =
effectie.CanCatch[F].catchNonFatalEither(fab())(f)
}

@SuppressWarnings(Array("org.wartremover.warts.DefaultArguments"))
Expand All @@ -95,8 +93,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchEitherF2[F[_], A, B](
private val ab: () => Either[A, B]
) extends AnyVal {
def apply(f: Throwable => A)(implicit EC: FxCtor[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatalEither(FxCtor[F].effectOf(ab()))(f)
def apply(f: Throwable => A)(implicit EC: effectie.FxCtor[F], CC: effectie.CanCatch[F]): F[Either[A, B]] =
effectie.CanCatch[F].catchNonFatalEither(effectie.FxCtor[F].effectOf(ab()))(f)
}

private[Catching] final class CurriedCanCatchEitherT1[F[_]](
Expand All @@ -109,8 +107,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchEitherT2[F[_], A, B](
private val fab: () => EitherT[F, A, B]
) extends AnyVal {
def apply(f: Throwable => A)(implicit CC: CanCatch[F]): EitherT[F, A, B] =
CanCatch[F].catchNonFatalEitherT(fab())(f)
def apply(f: Throwable => A)(implicit CC: effectie.CanCatch[F]): EitherT[F, A, B] =
CC.catchNonFatalEitherT(fab())(f)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ object ConsoleEffect {
def apply[F[_]: ConsoleEffect]: ConsoleEffect[F] =
implicitly[ConsoleEffect[F]]

implicit def consoleEffectF[F[_]: FxCtor: FlatMap]: ConsoleEffect[F] =
implicit def consoleEffectF[F[_]: effectie.FxCtor: FlatMap]: ConsoleEffect[F] =
new ConsoleEffectF[F]

final class ConsoleEffectF[F[_]: FxCtor: FlatMap] extends ConsoleEffectWithoutFlatMap[F] with ConsoleEffect[F] {
final class ConsoleEffectF[F[_]: effectie.FxCtor: FlatMap] extends ConsoleEffectWithoutFlatMap[F] with ConsoleEffect[F] {

@SuppressWarnings(Array("org.wartremover.warts.Recursion"))
override def readYesNo(prompt: String): F[YesNo] = for {
_ <- putStrLn(prompt)
answer <- readLn
yesOrN <- answer match {
case "y" | "Y" =>
FxCtor[F].effectOf(YesNo.yes)
effectie.FxCtor[F].effectOf(YesNo.yes)
case "n" | "N" =>
FxCtor[F].effectOf(YesNo.no)
effectie.FxCtor[F].effectOf(YesNo.no)
case _ =>
readYesNo(prompt)
}
Expand Down
17 changes: 9 additions & 8 deletions cats-effect/src/main/scala-2/effectie/cats/Effectful.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package effectie.cats

import effectie.cats.Effectful._


trait Effectful {

def effectOf[F[_]]: CurriedEffectOf[F] = new CurriedEffectOf[F]

def pureOf[F[_]]: CurriedEffectOfPure[F] = new CurriedEffectOfPure[F]

def unitOf[F[_]: FxCtor]: F[Unit] = FxCtor[F].unitOf
def unitOf[F[_]: effectie.FxCtor]: F[Unit] = effectie.FxCtor[F].unitOf

def errorOf[F[_]]: CurriedErrorOf[F] = new CurriedErrorOf[F]

@deprecated(message = "Use pureOf instead.", since = "1.4.0")
@inline final def effectOfPure[F[_]]: CurriedEffectOfPure[F] = pureOf[F]

@deprecated(message = "Use unitOf instead", since = "1.4.0")
@inline final def effectOfUnit[F[_]: FxCtor]: F[Unit] = unitOf[F]
@inline final def effectOfUnit[F[_]: effectie.FxCtor]: F[Unit] = unitOf[F]

}

Expand All @@ -26,22 +27,22 @@ object Effectful extends Effectful {
private[Effectful] final class CurriedEffectOf[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](a: => A)(implicit EF: FxCtor[F]): F[A] =
FxCtor[F].effectOf(a)
def apply[A](a: => A)(implicit EF: effectie.FxCtor[F]): F[A] =
effectie.FxCtor[F].effectOf(a)
}

private[Effectful] final class CurriedEffectOfPure[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](a: A)(implicit EF: FxCtor[F]): F[A] =
FxCtor[F].pureOf(a)
def apply[A](a: A)(implicit EF: effectie.FxCtor[F]): F[A] =
effectie.FxCtor[F].pureOf(a)
}

private[Effectful] final class CurriedErrorOf[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](throwable: Throwable)(implicit EF: FxCtor[F]): F[A] =
FxCtor[F].errorOf(throwable)
def apply[A](throwable: Throwable)(implicit EF: effectie.FxCtor[F]): F[A] =
effectie.FxCtor[F].errorOf(throwable)
}

}
18 changes: 1 addition & 17 deletions cats-effect/src/main/scala-2/effectie/cats/Fx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package effectie.cats
import cats.effect.IO
import cats.Id

import scala.concurrent.{ExecutionContext, Future}

trait Fx[F[_]] extends effectie.Fx[F] with FxCtor[F] with effectie.FxCtor[F] with CanCatch[F]

object Fx {
def apply[F[_]: Fx]: Fx[F] = implicitly[Fx[F]]
type Fx[F[_]] = effectie.Fx[F]

implicit object IoFx extends Fx[IO] {

Expand All @@ -27,18 +23,6 @@ object Fx {
CanCatch.CanCatchIo.catchNonFatalThrowable(fa)
}

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
implicit def futureFx(implicit EC: ExecutionContext): Fx[Future] = new FutureFx

final class FutureFx(implicit override val EC0: ExecutionContext)
extends Fx[Future]
with FxCtor[Future]
with effectie.FxCtor.FutureFxCtor
with effectie.CanCatch.CanCatchFuture {
override def catchNonFatalThrowable[A](fa: => Future[A]): Future[Either[Throwable, A]] =
CanCatch.canCatchFuture.catchNonFatalThrowable(fa)
}

implicit object IdFx extends Fx[Id] {

@inline override final def effectOf[A](a: => A): Id[A] = a
Expand Down
10 changes: 1 addition & 9 deletions cats-effect/src/main/scala-2/effectie/cats/FxCtor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@ package effectie.cats
import cats.Id
import cats.effect.IO

import scala.concurrent.{ExecutionContext, Future}

trait FxCtor[F[_]] extends effectie.FxCtor[F]

object FxCtor {
def apply[F[_]: FxCtor]: FxCtor[F] = implicitly[FxCtor[F]]
type FxCtor[F[_]] = effectie.FxCtor[F]

implicit final val ioFxCtor: FxCtor[IO] = Fx.IoFx

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
implicit def futureFxCtor(implicit EC: ExecutionContext): FxCtor[Future] =
Fx.futureFx

implicit final val idFxCtor: FxCtor[Id] = Fx.IdFx

}
24 changes: 24 additions & 0 deletions cats-effect/src/main/scala-2/effectie/cats/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package effectie

import _root_.cats.data.EitherT

/** @author Kevin Lee
* @since 2021-11-14
*/
package object cats {

implicit final class CanCatchOps[F[_]](private val canCatch: effectie.CanCatch[F]) extends AnyVal {

def catchNonFatalEitherT[A, AA >: A, B](fab: => EitherT[F, A, B])(f: Throwable => AA): EitherT[F, AA, B] =
EitherT(canCatch.catchNonFatalEither[A, AA, B](fab.value)(f))

}

implicit final class FxhOps[F[_]](private val fx: Fx[F]) extends AnyVal {

def catchNonFatalEitherT[A, AA >: A, B](fab: => EitherT[F, A, B])(f: Throwable => AA): EitherT[F, AA, B] =
EitherT(fx.catchNonFatalEither[A, AA, B](fab.value)(f))

}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package effectie.cats.syntax

import cats.data.EitherT
import effectie.CanCatch
import effectie.cats.syntax.error.{EitherTFABErrorHandlingOps, FAErrorHandlingOps, FEitherABErrorHandlingOps}
import effectie.cats.{CanCatch, CanHandleError, CanRecover}
import effectie.cats.{CanHandleError, CanRecover}
import effectie.cats._

/** @author Kevin Lee
* @since 2021-10-16
Expand Down
32 changes: 2 additions & 30 deletions cats-effect/src/main/scala-3/effectie/cats/CanCatch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@ import scala.concurrent.{ExecutionContext, Future}
/** @author Kevin Lee
* @since 2020-06-07
*/
trait CanCatch[F[_]] extends effectie.CanCatch[F] {

override type XorT[A, B] = EitherT[F, A, B]

inline override final protected def xorT[A, B](fab: F[Either[A, B]]): EitherT[F, A, B] = EitherT(fab)

inline override final protected def xorT2FEither[A, B](efab: EitherT[F, A, B]): F[Either[A, B]] = efab.value

}

object CanCatch {
def apply[F[_]: CanCatch]: CanCatch[F] = summon[CanCatch[F]]

type CanCatch[F[*]] = effectie.CanCatch[F]

given canCatchIo: CanCatch[IO] with {

Expand All @@ -33,25 +24,6 @@ object CanCatch {

}

given canCatchFuture(using EC: ExecutionContext): CanCatch[Future] =
new effectie.CanCatch.CanCatchFuture with CanCatch[Future] {

override val EC0: ExecutionContext = EC

override def catchNonFatalThrowable[A](fa: => Future[A]): Future[Either[Throwable, A]] =
fa.transform {
case scala.util.Success(a) =>
scala.util.Try[Either[Throwable, A]](Right(a))

case scala.util.Failure(scala.util.control.NonFatal(ex)) =>
scala.util.Try[Either[Throwable, A]](Left(ex))

case scala.util.Failure(ex) =>
throw ex
}(EC0)

}

given canCatchId: CanCatch[Id] with {

inline override final def mapFa[A, B](fa: Id[A])(f: A => B): Id[B] = f(fa)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import scala.util.control.NonFatal
/** @author Kevin Lee
* @since 2020-08-17
*/
trait CanHandleError[F[_]] extends effectie.CanHandleError[F] {
trait CanHandleError[F[*]] extends effectie.CanHandleError[F] {

type XorT[A, B] = EitherT[F, A, B]

Expand All @@ -24,7 +24,7 @@ trait CanHandleError[F[_]] extends effectie.CanHandleError[F] {

object CanHandleError {

def apply[F[_]: CanHandleError]: CanHandleError[F] = summon[CanHandleError[F]]
def apply[F[*]: CanHandleError]: CanHandleError[F] = summon[CanHandleError[F]]

given ioCanHandleError: CanHandleError[IO] with {

Expand Down

0 comments on commit 401adc1

Please sign in to comment.