Am I missing a `Mutex.withLock` pattern that suppo...
# coroutines
j
Am I missing a
Mutex.withLock
pattern that supports a suspending body? Aside from just writing it myself, of course. Is there some anti-pattern or gotcha here that prevents it from being provided out-of-the-box?
z
It’s inline, so it should allow suspending calls inside its body. This compiles for me:
Copy code
suspend fun foo() {
    val m = Mutex()
    m.withLock {  
      delay(1000)
    }
  }
j
ah. i didn't even try
i just refactored away my need for the mutex!
anyway thanks
i'm not sure i knew this was a thing we could do, but it certainly makes sense
d
I was surprised when I noticed some time ago
l
That's what allows you to call suspending functions from scope functions (
also
,
apply
, etc.), and that's also why there's an
inline
keyword, not just a compiler optimization.