Use with a car wedge in Kotlin

In stdlib / kotlin.io we find

inline fun <T : Closeable?, R> T.use(block: (T) -> R): R (source) 

However, it is implemented on Closeable, and not on the AutoCloseable superinterface. When working with some frameworks using AutoCloseable, this can be a little painful.

Why doesn't Kotlin support โ€œuseโ€ with AutoCloseble?

+5
source share
1 answer

kotlin-stdlib intended for use with JDK 6 and above, so it does not know AutoCloseable (only added in Java 7).

But you can find the desired function kotlin-stdlib-jre7 , the stdlib extension for JDK 7 for Kotlin 1.1 has been added. You can replace the kotlin-stdlib dependency kotlin-stdlib , since it depends on the most basic stdlib itself.

It is defined as:

 public inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R { ... } 

(api link) (github source)

And it was first mentioned in this blog post .

+6
source

Source: https://habr.com/ru/post/1265238/


All Articles