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 FiberLocal.Make #1821

Open
kubukoz opened this issue Mar 26, 2021 · 6 comments
Open

Add FiberLocal.Make #1821

kubukoz opened this issue Mar 26, 2021 · 6 comments

Comments

@kubukoz
Copy link
Member

kubukoz commented Mar 26, 2021

Follow-up to #1393.

I think users will likely want to create fiber locals for an abstract effect. We can surely create them in IO and other effects that have this functionality, it is probably possible for monad transformers too (I might be missing something).

It would be great to have an abstraction for this that people can depend on instead of sticking to IO:

object FiberLocal {
  trait Make[F[_]] {
    def make(a: A): F[FiberLocal[F, A]]
  }

  object Make {
    implicit val makeForIO: Make[IO] = ...
    
    implicit def makeForOptionT[F[_]: Make]: Make[OptionT[F, *]] = ...
  }
}
@djspiewak
Copy link
Member

I'm definitely not a fan of this. The problem is that this is a false abstraction: fiber locals in other IOs behave entirely differently from CE3's, and from each other. Users can't meaningfully write code which is generic over all of them. I would rather punt the problem downstream a bit and allow users to create a la carte abstractions (similar to UnsafeRun in weaver and in cats-effect-testing) which meet their needs directly when they want to abstract over the different implementations.

@RaasAhsan
Copy link

@djspiewak Not sure if you caught it in the PR, but I ended up parameterizing FiberLocal over F and added a specialized constructor for IO in the companion object. That way we're not committing to creating a type class for it today, but if it becomes feasible in the future then we could without much breakage. Though I think we would need to move it into std for that to work. How do you feel about that?

@djspiewak
Copy link
Member

Uh. Hmm. I'll need to think about it a bit. I really don't think it's going to be something we want to abstract over, but I see what you're saying about binary compatibility.

@RaasAhsan
Copy link

RaasAhsan commented Mar 27, 2021

My original thinking was that our definition of FiberLocal is so simple that Monix and ZIO can implement it with a subset of their functionality i.e. all they need is copy-on-fork semantics, which I think they have. Need to double check. FiberRef goes further and allows shared scopes, but in a completely compositional manner that doesn't demand any extra runtime behavior (via Ref)

@oleg-py
Copy link
Contributor

oleg-py commented Mar 28, 2021

Monix and ZIO can implement it with a subset of their functionality i.e. all they need is copy-on-fork semantics, which I think they have

We definitely don't have copy-on-fork in monix. We opted for making user isolate region explicitly to have CE2 laws like fa.start.flatMap(_.join) <-> fa when fa is mutating local context

@kubukoz
Copy link
Member Author

kubukoz commented Apr 4, 2021

Related: #1847

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants