Destructors or multi-platform `Closeable`/`Disposa...
# language-proposals
d
Destructors or multi-platform `Closeable`/`Disposable` interface. Just to provide some standard way of specifying resources to be released by the user and not necessarily the runtime. A class/object with a destructor would pretty much behave like a class that implements
AutoCloseable
, which I think should be the JVM implementation of it.
Also, I realize that 'Destructor' may not be the appropriate term for what I describe.
a
can you give us a pseudo-code example of how it should work?
d
Copy code
class Texture(path: String) {
    val texId: Int

    init {
        texId = glGenTexture()
        // Read from file.....
    }

    deinit {
        glDestroyTexture(texId)
    }
}
Copy code
val sprite = Texture("res/image.png")
// use image in game.
free sprite
s
You might want to create an issue or try #stdlib
a
@Dominaezzz and when should this
deinit
be called?
d
Whenever the user doesn't need the object anymore.
GC doesn't have be involved.
d
Why is this better than a
Closeable
interface?
s
I think he just wants
AutoCloseable
in common code
✔️ 1
d
It's not. I was proposing a multi-platform version.
Closeable
is only available on the JVM back-end.
d
Your code above used some new keywords and language constructors, that's why I was confused.
A common
AutoCloseable
is a good idea.