Skip to content

Add different effect init for Random/SecureRandom #4410

@geny200

Description

@geny200

I would like to be able to initialize Random in a different effect, as it's in Ref and Deffered (def in[F, G, A]). Motivation:

  • I can't use transformer implicits, because I want to initialize Random, for example, in ConnectionIO (type from Doobie, which has implementation of Sync for ConnectionIO).
  • I don't want to use mapK because it would be incorrect and expensive for implementation (ex: provided by lib mapK for ConnectionIO uses a Dispatcher that runs the logic, which adds additional costs for lift and unlift from IO)

Below is a sample code for Ref, I would like to be able to do exactly the same for Random and SecureRandom.

  import cats.FlatMap
  import cats.effect.std.Console
  import cats.effect.{Ref, Sync}
  import cats.syntax.all.*

  class Foo[F[_]: FlatMap: Console](ref: Ref[F, Int]) {
    def print: F[Unit] = ref.get.flatMap(Console[F].println)
  }

  def foo[I[_]: Sync, F[_]: Sync: Console]: I[Foo[F]] =
    for {
      ref <- Ref.in[I, F, Int](0)
      /// Init some classes in F
    } yield new Foo(ref)

Proposal:

I looked at current implementation javaSecuritySecureRandom, and it can be divided into 2 effects. So, new method will have signature like this:

// Proposal - add new method
def javaSecuritySecureRandomIn[I[_]: Sync, F[_]: Sync](n: Int): I[SecureRandom[F]]

// Current impl
def javaSecuritySecureRandom[F[_]: Sync](n: Int): F[SecureRandom[F]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions