2 ms·
The error was probably trying to write a generic `using`. In my experience languages which use higher order functions or macros for scope cleanup tend to build
by masklinn 1y ago
The error was probably trying to write a generic `using`. In my experience languages which use higher order functions or macros for scope cleanup tend to build high-level utilities directly onto the lowest level features, it can be a bit repetitive but usually not too bad.
So in this case, rather than a generic `using` built on the even more generic `try/except` you should probably have built a `withFile` callback. It's a bit more repetitive, but because you know exactly what you're working with it's a lot less error prone, and you don't need to hope there's a ready made protocol.
It also provides the opportunity of upgrading the entire thing e.g. because `withFile` would be specialised for file interaction it would be able to wrap all file operations as promise-based methods instead of having to mix promises and legacy callbacks.
- davidmurdoch 1y agoSure, the with* pattern is fine. I was just playing with the idea of C#s disposable pattern in JS.