diff --git a/std/shared/src/main/scala/cats/effect/std/MapRef.scala b/std/shared/src/main/scala/cats/effect/std/MapRef.scala index 17b8d4ac97..cf3e900916 100644 --- a/std/shared/src/main/scala/cats/effect/std/MapRef.scala +++ b/std/shared/src/main/scala/cats/effect/std/MapRef.scala @@ -45,6 +45,20 @@ trait MapRef[F[_], K, V] extends Function1[K, Ref[F, V]] { object MapRef extends MapRefCompanionPlatform { + /** + * Default constructor for [[MapRef]]. If [[Sync]] is available, it will delegate to + * [[ofConcurrentHashMap]], otherwise it will fallback to [[ofShardedImmutableMap]]. + */ + def apply[F[_]: Concurrent, K, V]: F[MapRef[F, K, Option[V]]] = { + Concurrent[F] match { + case s: Sync[F] => + ofConcurrentHashMap()(s) + case _ => + ofShardedImmutableMap[F, K, V](shardCount = Runtime.getRuntime.availableProcessors()) + } + + } + /** * Creates a sharded map ref to reduce atomic contention on the Map, given an efficient and * equally distributed hash, the contention should allow for interaction like a general @@ -522,5 +536,8 @@ object MapRef extends MapRefCompanionPlatform { val (set, out) = f(v) (set.some, out.some) } + + def withDefaultValue(default: V)(implicit E: Eq[V], F: Functor[F]): MapRef[F, K, V] = + defaultedMapRef(mRef, default) } }