|
|
|
|
|
by pjmlp
7 days ago
|
|
I was talking in general, not JS specific, especially since JS has global namespace, note my second paragraph. In any case, you have two ways to do something similar, kind of. function with_file(filename, operation_fn) {
let resource = null;
try {
resource = open_resource(filename);
return operation_fn(resource);
} finally {
resource?.close()
}
}
with_file("/my/path/something", (fd) => {
let data = fd.read_something()
// handle data
})
Naturally there are multiple variations thereof, on how to handle errors, or which way to parameterise how to close resources.Then we have the C# inspired way, using fd = open_resource("/my/path/something")
let data = fd.read_something()
// handle data
And if open_resouce doesn't support using declaration, an helper builder function can be used to wrap it in such a type.Yes it isn't as automatic as pure RAII, but it gets close enough for most practical purposes. |
|