Created
May 3, 2016 15:27
-
-
Save crowjdh/1b026b0447ebd00b1b97968950526e6f to your computer and use it in GitHub Desktop.
Extension function expression for Closeable.use in Kotlin
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
| /** | |
| * Built-in "use" function: | |
| * resources.openRawResource(resId).use { inputStream -> | |
| * inputStream.doSomething1() | |
| * inputStream.doSomething2() | |
| * } | |
| * | |
| * New "use" function: | |
| * resources.openRawResource(resId).use { | |
| * this.doSomething1() | |
| * doSomething2() | |
| * } | |
| * | |
| * See this video for more information: https://youtu.be/A2LukgT2mKc?t=23m43s | |
| */ | |
| inline fun <T: Closeable, R> T.use(block: T.() -> R): R { | |
| use { closable -> | |
| return closable.block() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment