I'm looking at cats.effect.concurrent.Deferred
and noticed that all pure factory methods inside its companion object return the F[Deferred[F, A]]
, not just Deferred[F, A]
like
def apply[F[_], A](implicit F: Concurrent[F]): F[Deferred[F, A]] =
F.delay(unsafe[F, A])
but
/**
* Like `apply` but returns the newly allocated promise directly instead of wrapping it in `F.delay`.
* This method is considered unsafe because it is not referentially transparent -- it allocates
* mutable state.
*/
def unsafe[F[_]: Concurrent, A]: Deferred[F, A]
Why?
The abstract class
has two methods defined (docs omitted):
abstract class Deferred[F[_], A] {
def get: F[A]
def complete(a: A): F[Unit]
}
So even if we allocate Deferred
directly it is not clear how the state of Deferred
can be modified via its public method. All modification are suspended with F[_]
.
F[_]
) cannot be considered referential transparent. Is that correct? – Bujumbura