= Exercise 5.18
// Refs:
:url-base: https://github.com/fenegroni/TGPL-exercise-solutions
:workflow: workflows/Exercise 5.18
:action: actions/workflows/ch5ex18.yml
:url-workflow: {url-base}/{workflow}
:url-action: {url-base}/{action}
:badge-exercise: image:{url-workflow}/badge.svg?branch=main[link={url-action}]
{badge-exercise}
Without changing its behaviour, rewrite the `fetch` function to use `defer` to close the writable file.
== Test
We want to test that the file is correctly closed by defer.
We don't need to set up a server, and the file can be assumed to be local.
What we want is to make sure the file is closed correctly.
=== Implementation
We validate that defer works using an anonymous function to print a log line.
The output is validated using the 'deferred close call' sentence.
NOTE: A better strategy is to implement the OS file handle count.
Unfortunately this requires system calls to find out
if a file descriptor is in use by a process.