Closeable in common stdlib

Closeable and Closeable.use are a very standard pattern.
So, I am very curious as to why it has not been included in the common standard library.

Closing low-level resources is a common problem, especially in Kotlin/Native.
Some libraries, such as KotlinX/IO are providing their own Closeable / use, but I don’t want to depend on KotlinX/IO just for access to Closeable!

Therefore, I ended up duplicating Closeable / use in my own multiplatform libraries, which is by essence a very bad idea :frowning:.

I searched common Kotlin places (discuss, youtrack, github) and couldn’t find any mention of this.

Any reason why Closeable is NOT in the common standard library?

1 Like

I guess it just hasn’t been implemented yet. Kotlin native is still in experimental access and so are multiplatform projects. There are hundreds of useful classes and interfaces in java and extracting the essentials into common takes time.
Just create an youtrack-issue (https://youtrack.jetbrains.com/issues/KT) and it will probably be added in the future. And I guess if you want to speed it up also create a PR on github :wink:

Here it is :wink: https://youtrack.jetbrains.com/issue/KT-31066

1 Like

Would it be a bad practice to use DisposableHandle instead of Closeable as it is a KMP compatible and after one year we still don’t have Closeable in common KMP? :thinking:

3 Likes