Is it possible to write a function that does something only after its caller is finished?
I am writing a function that creates a temporary file, writes some stuff to it, and returns the filename. The expression where this is called gets the returned value, uses the file, but nobody does after that. The file should therefore be deleted, but by whom?
If I delete it inside the function that creates it, then the file can't be used by the expression that called it.
I can just delete the file on the next S-expression, right after the expression that calls the function, but that seems like a violation of modularity, since the creation and deletion are not in the same place (not to mention that this requires me to save the returned value, introducing a let).
It would be good if my function could stay in the background after returning, and somehow know when to delete the file. Of course, I would need to tell it when: however, something like "two levels up the call stack" sounds like a non-robust solution. Maybe you could have a scope for temporary files (i.e. a let): as soon as you fall out of that scope, all temporary files created inside get deleted.
Maybe there is a way of creating temporary files so that they get cleaned by a garbage collector as soon as the program ends.
--
This reminds me of an old debate, about a solution that seemed much easier by using GOTO. It was probably a reaction to "GOTO Considered Harmful".
I am writing a function that creates a temporary file, writes some stuff to it, and returns the filename. The expression where this is called gets the returned value, uses the file, but nobody does after that. The file should therefore be deleted, but by whom?
If I delete it inside the function that creates it, then the file can't be used by the expression that called it.
I can just delete the file on the next S-expression, right after the expression that calls the function, but that seems like a violation of modularity, since the creation and deletion are not in the same place (not to mention that this requires me to save the returned value, introducing a let).
It would be good if my function could stay in the background after returning, and somehow know when to delete the file. Of course, I would need to tell it when: however, something like "two levels up the call stack" sounds like a non-robust solution. Maybe you could have a scope for temporary files (i.e. a let): as soon as you fall out of that scope, all temporary files created inside get deleted.
Maybe there is a way of creating temporary files so that they get cleaned by a garbage collector as soon as the program ends.
--
This reminds me of an old debate, about a solution that seemed much easier by using GOTO. It was probably a reaction to "GOTO Considered Harmful".
(no subject)
Date: 2006-02-07 06:44 pm (UTC)In the case of temporary files, you'd simply attach the file closure/deletion code as a finalization of the file handle.
Finalization is available in Java (via implementation of the finalize() method provided by Object), OCaml (via the Gc.finalise library call), and possibly other GC'd languages.
Two problems with this: As far as I am aware, the semantics of finalization (especially with respect to what code can be executed during a finalization) are poorly specified. Additionally, finalization is limited by the garbage collector's ability to detect when a value is no longer reachable, and so all GC problems related to uncollected memory also apply to uninvoked finalizations.
(no subject)
Date: 2006-02-07 06:51 pm (UTC)Also, you can do something similar in C++ by explicitly extending the destructor of a class. However, this is not the same as finalization since the invocation of a destructor (at least for heap-allocated values) is under the programmer's control via the 'free' operator. Specifically, the programmer is allowed to free a value while it is still technically reachable -- this is a notorious source of memory errors in C++.
(no subject)
Date: 2006-02-07 07:29 pm (UTC)(no subject)
Date: 2006-02-08 01:31 pm (UTC)(no subject)
Date: 2006-02-07 09:24 pm (UTC)(no subject)
Date: 2006-02-07 11:05 pm (UTC)http://linux.omnipotent.net/article.php?article_id=11369
(no subject)
Date: 2006-02-08 01:34 pm (UTC)(no subject)
Date: 2006-02-08 06:02 am (UTC)(with-temporary-file (filename) (do-stuff-to filename))
(no subject)
Date: 2006-02-08 06:10 am (UTC)File.open("file.txt") do |f|
f.readlines.each { |l| ... }
end
I think this ("inversion of control") is the Right Way to do it.
(no subject)
Date: 2006-02-08 10:58 am (UTC)Object Oriented:
Date: 2006-02-09 11:53 am (UTC)