interop-cats icon indicating copy to clipboard operation
interop-cats copied to clipboard

Add Dispatcher[Task] instance

Open renancaju opened this issue 3 years ago • 9 comments

Method toManaged requires this instance but it does not exist.

renancaju avatar Nov 25 '21 23:11 renancaju

Don't you actual want a dispatcher for F, the effect type you are converting from?

adamgfraser avatar Nov 26 '21 06:11 adamgfraser

Just to clarify - Dispatcher is not a type class so we can't have a generic instance. Creating a dispatcher returns a Resource which needs to be cleaned up after you're done using it. I struggled a lot with trying to make Dispatcher usage more ergonomic but I didn't come up with anything. There is definitely room for improvement here.

joroKr21 avatar Nov 26 '21 07:11 joroKr21

Fiddling around, I think I might have mixed up the uses of toManaged and toManagedZIO which does not need a Dispatcher. Am I right? In order to convert a cats Resource to a ZManaged can I just use toManagedZIO instead?

renancaju avatar Nov 26 '21 09:11 renancaju

Fiddling around, I think I might have mixed up the uses of toManaged and toManagedZIO which does not need a Dispatcher. Am I right? In order to convert a cats Resource to a ZManaged can I just use toManagedZIO instead?

Yes, that's why it exists. When the F[_] in Resource is itself ZIO we don't need Dispatcher and that makes it much easier to use. 👍

joroKr21 avatar Nov 26 '21 10:11 joroKr21

Cool. Out of curiosity, why would we need toManaged for a generic F[_] in a ZIO Interop project?

renancaju avatar Nov 26 '21 12:11 renancaju

I think for the people that mix ZIO with other effect types. Maybe we should have used the short name for ZIO and a longer name for generic effects 🤔 - but I think it's too late to change now.

joroKr21 avatar Nov 26 '21 12:11 joroKr21

Yeah I would actually think the toManaged variant would be more common where you had like a Resource[CIO, A] and wanted to convert it into a ZManaged. And that is where you would need a Dispatcher[CIO] which would need to come from Cats and not from ZIO.

adamgfraser avatar Nov 26 '21 21:11 adamgfraser

Yeah I would actually think the toManaged variant would be more common where you had like a Resource[CIO, A] and wanted to convert it into a ZManaged.

If you use only ZIO and tagless final libraries like http4s and doobie then you don't actually have this problem.

And that is where you would need a Dispatcher[CIO] which would need to come from Cats and not from ZIO.

Yeah, the issue is that the Dispatcher constructor gives you a Resource - so catch 22 🤔

joroKr21 avatar Nov 26 '21 21:11 joroKr21

Yes we definitely want to support both styles.

If you are working with Dispatcher and your application isn't ultimately a Cats Effect application then you are going to have to unsafely run creating a dispatcher somewhere to use in nested scopes.

adamgfraser avatar Nov 26 '21 21:11 adamgfraser