I’ve run into an interesting issue recently.
The method signature for use
is inline fun <T : Closeable?, R> T.use(block: (T) -> R): R
Because it works on Closeable?
, there are two null-checks required. This adds unnecessary instructions to the byte code, and causes FindBugs to warn me.
I’m not sure I understand why you’d want the block to run if the Closeable
was null, versus doing something like closeable?.use { ... }
.
I considered adding my own inline extension function that requires a Closeable
, but then that needs to be maintained.
Am I missing a better option here? Would it be reasonable for the compiler to see that a non-null type is being passed in, and turn the two this?.close()
calls into this.close()
?