Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discard method as a safer alternative to void #1001

Merged
merged 5 commits into from
Aug 29, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions modules/higherKindCore/src/main/scala/tofu/syntax/monadic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ object monadic extends TupleSemigroupalSyntax with ApplicativeSyntax with MonadS
def unit[F[_]](implicit F: Applicative[F]): F[Unit] = F.unit

implicit final class TofuFunctorOps[F[_], A](private val fa: F[A]) extends AnyVal {
/** Safer way than [[void]] to discard a value
*
* Explicit type ascription eliminates a bug when one can begin discarding non-discard-able value.
*
* @example
* {{{
* val computation1: F[Thing] = ???
* val result11: F[Unit] = computation1.void
* val result12: F[Unit] = computation1.discard[Thing]
*
* //after changes or refactoring
* val computation2: F[F[Thing]] = ???
* val result21: F[Unit] = computation2.void //error here, nested F is not evaluated and compiler doesn't warn
* val result22: F[Unit] = computation2.discard[Thing] //compiler produces an error
* }}} */
def discard[AA](implicit ev: AA =:= A, F: Functor[F]): F[Unit] = F.void(fa)
def map[B](f: A => B)(implicit F: Functor[F]): F[B] = F.map(fa)(f)
def fmap[B](f: A => B)(implicit F: Functor[F]): F[B] = F.fmap(fa)(f)
def widen[B >: A](implicit F: Functor[F]): F[B] = F.widen(fa)
Expand Down