I'm trying to make this higher-order Kotlin function:
private inline fun <T> reentrant(entered: ThreadLocal<Boolean>, block: () -> T, enter: (() -> T) -> T) =
if (entered.get()) block()
else {
try {
entered.set(true)
enter(block)
} finally {
entered.set(false)
}
}
To be used like this, for example:
reentrant(fooing, block) { b ->
try {
log("Entering a state of foo")
b()
// sidenote: while writing this question it dawned on me,
// that i could just calll the captured `block` here,
// as a workaround
} finally {
log("Foo nevermore")
}
}
However, apparently the construct enter(block) is not allowed (Illegal usage of inline parameter block).
Since everything here is inline, I think it should be technically possible. Is this feature just not supported by the compiler? Or is there a way I can do this after all?
blockparameter it now compiles. Do you want to avoid this, or is that good enough?