Daniel Pitts
01/14/2024, 11:04 PMDaniel Pitts
01/14/2024, 11:05 PMimport 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.
}
Kroppeb
01/18/2024, 8:56 AM