Skip to content

Commit bef2598

Browse files
halotukozakclaude
andcommitted
fix: implement Semaphore.mapK instead of throwing UnsupportedOperationException
Delegate each Semaphore operation through the provided FunctionK, as suggested in PR review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3bf8aa5 commit bef2598

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

monix-catnap/shared/src/main/scala/monix/catnap/Semaphore.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,20 @@ final class Semaphore[F[_]] private (provisioned: Long, ps: PaddingStrategy)(
208208
cats.effect.kernel.Resource.makeFull[F, Unit](poll => poll(acquire))(_ => release)
209209

210210
override def mapK[G[_]](f: cats.arrow.FunctionK[F, G])(
211-
implicit G: cats.effect.kernel.MonadCancel[G, _]): cats.effect.std.Semaphore[G] =
212-
throw new UnsupportedOperationException("Monix Semaphore does not support mapK")
211+
implicit G: cats.effect.kernel.MonadCancel[G, _]): cats.effect.std.Semaphore[G] = {
212+
val self = this
213+
new cats.effect.std.Semaphore[G] {
214+
def available: G[Long] = f(self.available)
215+
def count: G[Long] = f(self.count)
216+
def acquireN(n: Long): G[Unit] = f(self.acquireN(n))
217+
def tryAcquireN(n: Long): G[Boolean] = f(self.tryAcquireN(n))
218+
def releaseN(n: Long): G[Unit] = f(self.releaseN(n))
219+
def permit: cats.effect.kernel.Resource[G, Unit] = self.permit.mapK(f)
220+
def mapK[H[_]](g: cats.arrow.FunctionK[G, H])(
221+
implicit H: cats.effect.kernel.MonadCancel[H, _]): cats.effect.std.Semaphore[H] =
222+
self.mapK(f.andThen(g))
223+
}
224+
}
213225

214226
private[this] val underlying =
215227
new Semaphore.Impl[F](provisioned, ps)

0 commit comments

Comments
 (0)