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

Switch context syntax to With* typeclasses #726

Merged
merged 2 commits into from
Jul 26, 2021
Merged
Changes from 1 commit
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: 7 additions & 9 deletions modules/kernel/src/main/scala/tofu/syntax/context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,42 @@ package tofu.syntax
import cats.{FlatMap, ~>}
import tofu._

import tofu.kernel.types._

object context {
def context[F[_]](implicit ctx: Context[F]): F[ctx.Ctx] = ctx.context

def hasContext[F[_], C](implicit ctx: F HasContext C): F[C] = ctx.context
def hasContext[F[_], C](implicit ctx: F WithContext C): F[C] = ctx.context

def ask[F[_]] = new AskPA[F](true)

private[syntax] final class AskPA[F[_]](val __ : Boolean) extends AnyVal {
def apply[C, A](f: C => A)(implicit ctx: F HasContext C): F[A] = ctx.ask(f)
def apply[C, A](f: C => A)(implicit ctx: F WithContext C): F[A] = ctx.ask(f)
}

def askF[F[_]] = new AskFPA[F](true)

private[syntax] final class AskFPA[F[_]](val __ : Boolean) extends AnyVal {
def apply[C, A](f: C => F[A])(implicit ctx: F HasContext C, M: FlatMap[F]): F[A] = ctx.askF(f)
def apply[C, A](f: C => F[A])(implicit ctx: F WithContext C, M: FlatMap[F]): F[A] = ctx.askF(f)
}

def runContext[F[_]] = new RunContextPA[F](true)

private[syntax] final class RunContextPA[F[_]](val __ : Boolean) extends AnyVal {
def apply[A, C, G[_]](fa: F[A])(ctx: C)(implicit HP: HasProvide[F, G, C]): G[A] =
def apply[A, C, G[_]](fa: F[A])(ctx: C)(implicit HP: WithProvide[F, G, C]): G[A] =
HP.runContext(fa)(ctx)
}

def runContextK[F[_]] = new RunContextKPA[F](true)

private[syntax] final class RunContextKPA[F[_]](val __ : Boolean) extends AnyVal {
def apply[C, G[_]](ctx: C)(implicit HP: HasProvide[F, G, C]): F ~> G =
def apply[C, G[_]](ctx: C)(implicit HP: WithProvide[F, G, C]): F ~> G =
HP.runContextK(ctx)
}

implicit final class LocalOps[F[_], A, C](private val fa: F[A])(implicit loc: F HasLocal C) {
implicit final class LocalOps[F[_], A, C](private val fa: F[A])(implicit loc: F WithLocal C) {
def local(project: C => C): F[A] = loc.local(fa)(project)
}

implicit final class AltLocalOps[F[_], A](private val fa: F[A]) extends AnyVal {
def local_[C](project: C => C)(implicit loc: F HasLocal C): F[A] = loc.local(fa)(project)
def local_[C](project: C => C)(implicit loc: F WithLocal C): F[A] = loc.local(fa)(project)
}
}