It can be fairly easily implemented in julia using finalizers.
I think we should not have this as the default behavour, but enable it with a parameter (maybe a keyword arg).
The preferred way to use a tempfile should remain using a do-block.
But when you are making lots of tempfiles (like if using julia for shell-scripting glue),
sometimes nesting do-blocks more than 2 deep becomes unwieldy.
And one is rarely in much of a hurry to delete tempfiles anyway.
Python does this (on by default, option delete=false
to disable).
It automatically deleted tempfiles when they are closed, and automatically closes the tempfile when it is garbage collected.
Even more useful with temporary directories. I've wanted this recently since written a number of tests that generate a lot of temporary directories with files in them and while noodling around on the REPL, you tend to not clean them up ever. If the GC did it for you, it would be ideal.
Most helpful comment
Even more useful with temporary directories. I've wanted this recently since written a number of tests that generate a lot of temporary directories with files in them and while noodling around on the REPL, you tend to not clean them up ever. If the GC did it for you, it would be ideal.