typelevel / cats-effect

The pure asynchronous runtime for Scala
https://typelevel.org/cats-effect/
Apache License 2.0
2.03k stars 520 forks source link

Add default constructor for `MapRef` #4139

Open yisraelU opened 2 months ago

yisraelU commented 2 months ago

Adds an apply method for MapRef. If the Effect type is an instance of Async , the ConcurrentHashMap implementation will be used otherwise the sharedImmutableMap will be used with the number of shards set to the number of Processors determined by Runtime.getRuntime.availableProcessors. The goal of this MR is to improve the UX for a user by having a sane default

closes #4136

P.S. as this is a first-time PR to this repo, please let me know if there are any conventions/practices, etc.. that I should be adhering to. TY

yisraelU commented 2 months ago

While this alone is great. Wwe may also request a default value in apply to improve UX as suggested by @Jasper-M

I have added an overloaded apply . WDYT

BalmungSan commented 2 months ago

I have added an overloaded apply . WDYT

Sadly I don't think that would work well: https://scastie.scala-lang.org/BalmungSan/WHoq2ShkTCm3QSs5puHixw

I think the best would be to do what Arman suggested and rather add a withDefault (better names are welcome) extension method to MapRefOptionOps. So that users can do:

val m1 = MapRef[IO, K, V] // Returns MapRef[IO, K, Option[V]
val m2 = m1.withDefault(foo) // Returns MapRef[IO, K, V]
yisraelU commented 2 months ago

I have added an overloaded apply . WDYT

Sadly I don't think that would work well: https://scastie.scala-lang.org/BalmungSan/WHoq2ShkTCm3QSs5puHixw

I think the best would be to do what Arman suggested and rather add a withDefault (better names are welcome) extension method to MapRefOptionOps. So that users can do:

val m1 = MapRef[IO, K, V] // Returns MapRef[IO, K, Option[V]
val m2 = m1.withDefault(foo) // Returns MapRef[IO, K, V]

I see, it fails for certain types. If we go back to empty parens for the apply method it should work. I'm happy with the extension method solution though, and I have nothing better than withDefault :)