There is an interesting pattern emerging. Usually code
is written as follows, for example:
setup_call_cleanup(
open(Path, read, Input),
read_term(Input, Term, Options),
close(Input))
But then here and then one finds the following variant.
This is often practiced by Logtalk code, maybe because
setup_call_cleanup/3 is not available everywhere:
open(Path, read, Input),
catch(read_term(Input, Term, Options), Error,
(close(Input),throw(Error))),
close(Input)
Using the alternative idiom is viable when the called goal
is deterministic, so that the cleanup is called anyway immediately
and no continuation handling is needed.
Some question:
-
Would it make sense to put the Logtalk pattern into
a library and give it a meta-predicate wrapping?
Which one performs better? -
How would we solve that the Logtalk pattern has a small
defect, i.e. an interrupt might happen before the catch/3, so
that the cleanup isn’t called.