Relax type parameter constraint for Closeable.use and AutoCloseable.use to allow nullable receiver.

#KT-12894 Fixed
This commit is contained in:
Ilya Gorbunov
2016-06-29 17:19:57 +03:00
parent e04873957e
commit 22ee3a8dc1
5 changed files with 68 additions and 6 deletions
+3 -3
View File
@@ -152,17 +152,17 @@ public fun URL.readBytes(): ByteArray = openStream().use { it.readBytes() }
* @return the result of [block] function on this closable resource.
*/
@kotlin.internal.InlineOnly
public inline fun <T : Closeable, R> T.use(block: (T) -> R): R {
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
var closed = false
try {
return block(this)
} catch (e: Throwable) {
closed = true
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
platformCloseSuppressed(this, e)
if (this != null) platformCloseSuppressed(this, e)
throw e
} finally {
if (!closed) {
if (this != null && !closed) {
close()
}
}