I've created a simple library to manage resources ...
# codereview
d
I've created a simple library to manage resources (similar to RAII from C++). It's like try-with-resource, but I think better. I would like some feedback if anyone is willing: https://github.com/StochasticTinkr/resourcescope. Example usage in 🧵
Copy code
import com.stochastictinkr.resourcescope.*
import java.io.FileReader
import org.lwjgl.glfw.GLFW.*

fun main() {
    resourceScope {
        // No need to hold on to the resource, it will be closed when the scope ends
        construct { glfwInit() } finally ::glfwTerminate
        
        val fileResource = constructClosable { FileReader("test.txt") }
        val file = fileResource.value
        val lines = file.readLines()
        // This could also have been written as:
        // val lines = constructClosable { FileReader("test.txt") }.value.readLines()
        // or as 
        // val (file, resource) = constructClosable { FileReader("test.txt") } // Destructuring for convenience
        // val lines = file.readLines()
    }
    // Resources are all closed at this point.
}
k
Are resources released in order or in the reverse order?