4 ms·
In functional languages you often see functions like with_file that use a closure to denote the file lifetime: with_file(fun file -> use_file(file)
by ufo 12y ago
In functional languages you often see functions like with_file that use a closure to denote the file lifetime:
with_file(fun file ->
use_file(file)
)
I wonder if there is a way to code something similar that works with "defer". This would free you from needing to "remember" to add the defer and would have the advantage of allowing you to use `return` and `break` statements which don't work if you wrap them inside an inner function.
The only thing I can think of is Python's with statement. Since its language syntax it allows for `return` and `break` but I wonder if there is a way to play nice with defer instead of requiring you to use another "block level" syntax construct for every file you use.
- bjz_ 12y agoWe used to do this in Rust, but have found RAII much better for this. Dealing with multiple resources open at once can quickly become painful. See http://blog.skylight.io/rust-means-never-having-to-close-a-socket/#facilitiesinotherlanguages http://blog.skylight.io/rust-means-never-having-to-close-a-s...
- Too 12y agoSounds like the finally-keyword in most languages?