Paul Woitaschek
02/02/2025, 9:59 AMpublic inline fun <T> networkResult(action: () -> T): NetworkResult<T> {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}
return try {
NetworkResult.Worked(action())
} catch (e: Exception) {
val failure = e.asFailureOrThrow
NetworkResult.Failed(failure)
}
}
w: NetworkResult.kt175 Wrong invocation kind ‘EXACTLY_ONCE’ for ‘action: () -> T’ specified, the actual invocation kind is ‘AT_MOST_ONCE’.
But the compiler is wrong, is it? This is EXACTLY_ONCE
Youssef Shoaib [MOD]
02/02/2025, 10:02 AMEXACTLY_ONCE
means that the block runs fully to completion, and if it fails, this function fails too. It's confusing, I know. Basically, for any EXACTLY_ONCE
function, this should be correct:
val foo: Int
networkResult {
...
foo = 5
...
}
// foo must be guaranteed to be set here.
Paul Woitaschek
02/02/2025, 10:06 AMA function parameter will be invoked exactly one time.
Should this then be better reflected in the docs? What are the implications of me declaring this wrongly?Youssef Shoaib [MOD]
02/02/2025, 10:16 AMfoo
even tho it might not be initialized.