Is there an updated document detailing the semantics of contracts? The Keep is from 5 years ago, and doesn't go in depth about calls-in-place contracts.
➕ 1
b
Ben Woodworth
11/16/2024, 7:30 PM
I know there's this issue about better explaining what calls-in-place actually means:
https://youtrack.jetbrains.com/issue/KT-43092#proposal-summary
I was surprised at first when K2 complained about a calls-in-place lambda being called in a try block. Or something similar, I forget. But the discussion is a good read, and highlights some things that aren't in the KEEP
y
Youssef Shoaib [MOD]
11/16/2024, 7:34 PM
Yes I just wish for a direct explanation of what the expected semantics are. The approximation I'm using right now is that calls-in-place is all about what happens after this function call. It says that all blocks will have finished, and won't be called after the function is done. Additionally, Exactly-once says that that block will be called, and only one time, and if it results in an exception, some exception should happen (but not necessarily the same one).
e
ephemient
11/16/2024, 9:38 PM
I haven't seen documentation but my interpretation is that they make possible
Copy code
// AT_MOST_ONCE
val x: Int
f { x = 1 }
Copy code
// AT_LEAST_ONCE
var x: Int
f { x = 1 }
println(x)
Copy code
// EXACTLY_ONCE
val x: Int
f { x = 1 }
println(x)
because that's the only reason I can see for them to exist
ephemient
11/16/2024, 9:43 PM
and for all of them including
UNKNOWN
, I'd expect
Copy code
// callsInPlace(block, any())
var x: Any? = null
f { x = 1 }
if (x is Int) {
// x can be smart-cast because it won't change again
to work, although in practice that seems more fragile to other conditions