Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Created June 3, 2024 03:40
Show Gist options
  • Save yongjhih/d55eb355104e3d3cb20b97fe0c5d6a70 to your computer and use it in GitHub Desktop.
Save yongjhih/d55eb355104e3d3cb20b97fe0c5d6a70 to your computer and use it in GitHub Desktop.
/**
* Compatibility
*
* See Also: androidx.core.content.res.use
*/
@OptIn(ExperimentalContracts::class)
inline fun <T: TypedArray, R> T.use(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return if (this is AutoCloseable) {
uses(block)
} else {
try { block(this) }
finally { recycle() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment