@stachenov if you really want it added, I think an effective path for getting it added would need to include discussion on the following aspects of the feature:
- Discussion on if the pain point (not having try-with-resources) is either impacting a wide range of use-cases or is highly impactful to a narrow range of use-cases.
- Discussion into if there is a deeper root pain point that could be addressed instead (Maybe something that addresses more than this one issue, e.x. non-local break/continue)
- Discussion into if there is a library solution or other minimum change solution that would be good enough (Could the problem be solved with just writing it in a less desired way?)
- Is there a large-change solution outside of the norm worth developing (let’s not get hyper-focused on one known solution if we could invent something even cooler)
- After all those discussions happen, it’s time to evaluate if it’s worth prioritizing over other features that could be worked on. If we don’t get significant gain from the change, or if the pain point is simply not a big or widespread issue, then it’s better to leave it for now.
IMO, these discussions must happen on some level for any feature to get added.
A few decent ideas have been brought up as alternative solutions to try-with-resources that need to be put through their paces:
- Using a labeled return
- Non-local break & continue
- Library / stdlib solution with
using
scope or ResourceHolder
IMHO, the existing labeled return is good enough for most use-cases. If we include a library solution and non-local break & continue, then odd cases of many resources also become good enough.
It’s fair to still feel that all of the existing solutions are in fact not good enough, but if you want to change it the feature will have to go through the process and overcome some healthy skepticism–it’s not enough to say “I’m familiar with it so we shouldn’t hold back in adding it”.
Maybe it’s worth pivoting the discussion to non-local break and continue instead? It could have the added benefit of solving problems we haven’t discussed unrelated to try-with-resources.
Here’s an example of having non-local continues:
val fileNames = listOf("file1", "mustBeEmpty", "file2")
for (fileName in fileNames) {
using { // Uses one scope just like try with resources
val out = Files.newOutputStream(fileSystem.getPath(fileName)).use() // Takes up the same line as the try-with-resources did.
if (fileName == "mustBeEmpty") continue // Non-local continue
out.write(byteArrayOf(1, 2, 3))
}
}
EDIT: If you like the above example and want to use it in your code now you can use labeled returns and it stays pretty clean. One cool spin on this is to remember that you can always use labels and return to perform a break or continue form a lambda based loop.
val fileNames = listOf("file1", "mustBeEmpty", "file2")
fileNames.forEach { fileName ->
using { // Uses one scope just like try with resources
val out = Files.newOutputStream(fileSystem.getPath(fileName)).use() // Takes up the same line as the try-with-resources did.
if (fileName == "mustBeEmpty") return@forEach // Or alternatively return@using
out.write(byteArrayOf(1, 2, 3))
}
}