F
is Sync
then you don't need () => F[Unit]
. It should be equivalent with F[Unit]
@Avasil bingo, thanks a lot :) Now it's giving unrelated error, which I expected :) neat
regarding () => F[Unit]
vs F[Unit]
I have have a def task[F[_]: Concurrent](): F[Unit] = ???
that I'm currently intending to pass to the loop. Would it be better to do loop(task())
rather than loop(task)
, or am I completely missing the point
Future
but fortunately that's not necessary with IO
implementations
F[_]
rather than IO
, cool I like that. But I ran in to an issue, mainly how to do this with a Future
from an external API? I cannot seem to find any general trait having a fromFuture
or similar. How is it best to approach this?
def foo[F[_]: Sync[F]]() = ???
over def foo[F[_]]()(implicit F: Sync[F])
I kind of like the flexibility by the implicit setup, but I was wondering because the tutorial use the former version. Am I doing it bad, is it taste, or?
implicit class richIODatasource[D <: DataSource, F[_]:Sync](val ids: F[D]) extends AnyVal {
import cats.effect.ExitCase.{Completed, Error, Canceled}
def use[T](f: Connection => F[T]): F[T] =
Resource.fromAutoCloseable[F, Connection](ids.map(_.getConnection())).use(c => f(c))
def transaction[T](f:Connection => F[T]):F[T] = use{ c =>
Sync[F].bracketCase(Sync[F].pure(c).map{c =>
c.setAutoCommit(false)
c
})(f){
case (in, Canceled | Error(_)) =>Sync[F].delay(in.rollback())
case (in,Completed) => Sync[F].delay(in.commit())
}
}
}
hmm does anyone have some advice/experience/best practices with how to write better tests with effects? Currently I'm doing mixing of for comprehensions and unsafeRunSync
, but it feels kind of awkward, I just cannot see how else to do it, e.g.:
"My app" should "do bar" in {
val barsF = for {
foo <- foo()
bars <- foo.bars()
} yield bars
val bars = barsF.unsafeRunSync
// tests go here
I'm struggling a bit, because in a way the cleanest would be to do the tests as F[_]
s too, such that they are just a part of the for comprehension, but in a way it also feels slightly off/awkward to me