https://kotlinlang.org logo
#announcements
Title
# announcements
p

Paul Woitaschek

07/04/2019, 7:23 PM
What's a possible use case for
InvocationKind.AT_MOST_ONCE
? Use case as in: What is it good for, not how to use it.
k

karelpeeters

07/05/2019, 7:55 AM
You can use it to avoid the "possible reassignment" error:
Copy code
fun main() {
    val bar: Int
    foo {
        bar = 5
    }
}

fun foo(block: () -> Unit) {
    contract { 
        callsInPlace(block, InvocationKind.AT_MOST_ONCE)
    }
    if (Random.Default.nextBoolean())
        block()
}
Altough I have no idea what that is useful for, it's not like you can actually use the
bar
variable afterwards because it may not be initialized.
9 Views