I am experimenting with <kotlinx.atomicfu.locks> a...
# multiplatform
d
I am experimenting with kotlinx.atomicfu.locks and found out that
Copy code
public expect inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T
as replacement of JVM
synchronized
misses
Copy code
contract {
    callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
making the compiler unable to infer this. Is it possible to combine
expect
functions with contacts?
it's possible to define it in the
actual
implementations, of course, but there's no guarantee that all of them respect the
contract
e
I think you'll have to use a wrapper for now,
Copy code
public inline fun <T> synchronized(lock: SynchronizedObject, block: () -> T): T {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return platformSynchronized(lock, block)
}
internal expect fun <T> platformSynchronized(lock: SynchronizedObject, block: () -> T): T
at least until https://youtrack.jetbrains.com/issue/KT-56127/K2-Provide-new-stable-syntax-for-contracts
d
indeed. I was hoping for a built-in solution, that would work for now. Thanks for the suggestion!