Avatar ·

How to properly use try-with-resources in Kotlin?

Hello everyone! I'm switching from Java to Kotlin and encountered a problem with resource management. In Java, I used to use try-with-resources for automatic closing of streams, files, etc. For example, in Java I would write: ```java try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) { String line = reader.readLine(); // processing } catch (IOException e) { e.printStackTrace(); } ``` In Kotlin, I see that there is no direct analogy of try-with-resources. I tried using regular try-catch-finally, but it looks cumbersome: ```kotlin val reader = BufferedReader(FileReader("file.txt")) try { val line = reader.readLine() // processing } catch (e: IOException) { e.printStackTrace() } finally { reader.close() } ``` Besides th