Cats-effect: Add sibling of `start` that returns a Resource

Created on 9 Sep 2019  路  7Comments  路  Source: typelevel/cats-effect

I think we can all agree fibers are hard to work with and probably what users shouldn't use 90% of the time. There are plenty of valid usecases at this moment though, for example starting an IO in background and cancelling it in case the application wraps up, which can be implemented with something like Resource(fa.start.fproduct(_.cancel)).

We should support that usecase and others and expose a method that'd look roughly like this:

def startResource: Resource[F, Fiber[A]]
//or
//the nested F is `join` without access to the finalizer
def startResource: Resource[F, F[A]]

in Concurrent or (as syntax in ConcurrentOps && as method on IO).

Name suggestions welcome - I'd go for background or startResource. Once we've figured out the appropriate shape and name I can provide an implementation.

Gitter microdiscussions:

Most helpful comment

馃憤 for the second variant

All 7 comments

馃憤 for the second variant

I think so as well. If someone wants to cancel from within, they can probably use a Deferred or something like that.

Eh, I think the first signature is a lot more intuitive and there's not much, if any, safety gained by hiding the cancel here. (Nah I'm not saying that because the first variant is in ZIO i swear on me mum)

The first signature does have the example of names, but I just feel it's really weird to make a resource-safe abstraction around something and then鈥β爀xpose the release action for that resource as a bare F[Unit]. I dunno. It's not too much of a problem since cancelation is idempotent, but still.

type JoinToken? ;)

LOL. I still really hate CancelToken, even though I see the value in it as a name. Confuses me every time.

For now I'll go with background because it's a meaningful name and it might be what people ctrl+F for (bonus points for not being startResource which I don't really like). We can bikeshed on the PR :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jatcwang picture jatcwang  路  4Comments

djspiewak picture djspiewak  路  4Comments

Avasil picture Avasil  路  3Comments

vasilmkd picture vasilmkd  路  9Comments

Daenyth picture Daenyth  路  3Comments