Created
June 3, 2024 03:40
-
-
Save yongjhih/d55eb355104e3d3cb20b97fe0c5d6a70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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