The compiler says this is wrong: ``` public inline...
# compiler
p
The compiler says this is wrong:
Copy code
public 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
y
EXACTLY_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:
Copy code
val foo: Int
networkResult {
  ...
  foo = 5
  ...
}
// foo must be guaranteed to be set here.
p
All the docs say is this (and that definitely is true for my code)
Copy code
A 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?
y
Yeah it really needs to be clarified better. The implications of a wrong declaration is that the example I gave would allow you to access
foo
even tho it might not be initialized.