All things considered, our experience in Scala Native has shown that resource management in Scala is way harder than it should be. This gist presents a simple design pattern that makes it resource management absolutely hassle-free: scoped implicit lifetimes.
The main idea behind it is to encode resource lifetimes through a concept of an implicit scope. Scopes are necessary to acquire resources. They are responsible for disposal of the resources once the evaluation exits the demarkated block in the source code.
- File handles
- Network connections
- Locks
- Off-heap memory allocations
- Thread pools
- Actor systems
- OpenGL Contexts
- ...
Your implementation above has a couple of different issues which result in resource leaks (per the exact specification of the java.lang.AutoCloseable documentation). Additionally, it doesn't reliably surface what and where a non-close exception blew up the scope.
For example, you call AutoCloseable.close even if the resource has not been successfully opened. This is clearly violating the explicit contract of AutoCloseable.close. There is a possibility of an AutoCloseable being "opened" and then an exception being thrown before the apply method of Scope successfully executes (imagine a System level exception akin to an OutOfMemory exception which must be assumed between EVERY single atomic execution of a code on the JVM) the resource's close command.
Additionally, it becomes confusing to use Scope if there is intermixed nesting where one resource must be closed before another one can be opened (imagine your w1 needed to be closed to reclaim resource space prior to engaging w2 or a like a FileWriter depending upon a BufferedWriter...and then imagine this with more than two resources which might have some sort of interaction more complex than simple linear nesting).
I believe your solution can be "fixed" to resolve the resource leak issue but will still remain cumbersome given any non-linear nesting pattern. However, I don't have time at the moment to dive in and fix it.
In an answer to a StackOverflow question, I came up with a much simpler FP immutable single method approach to handing this issue including arbitrary nesting. IOW, it doesn't need any sort of var to hold onto state for managing the future close operations. You can use the contents of this answer to refactor yours to eliminate the leaks. And I will try to revisit sometime in the future and actually "refactor" your solution to handle the leak.