When I worked on generators I contemplated adding protocol to invoke optional procedures such as clean up procedure, but then it would lose its simplicity (e.g. if generators are chained, it's not obvious how the 'cleanup' signal should be propagated).  So I decided resource management should be separated, if any.

Possible options would be:

(make-directory-generator <directory>) => generator, clean-up-thunk
where directory would be closed if generator is exhausted or clean-up-thunk is called

or

(with-directory-generator <directory> (lambda (generator) ...))
where directory would be closed if closure returns normally or raises an error.



On Wed, Dec 11, 2019 at 5:59 AM John Cowan <xxxxxx@ccil.org> wrote:
There is inherently no answer to "where do we draw the lines?" except good taste.

I wish there were a standard way to tell generators to clean up their state, if any.

On Wed, Dec 11, 2019 at 9:13 AM <xxxxxx@ancell-ent.com> wrote:
> From: Lassi Kortela <xxxxxx@lassi.io>
> Date: Wednesday, December 11, 2019 4:58 AM
>
>>> I like directory-files because it's the dead simple API: directory
>>> path in, list of entries out (dot-files optional).  It is of course
>>> redundant with the generator interface, and would not be the end of
>>> the world to omit.
>
> We all like it, but it's up for debate where it should go. IMHO
> higher-level niceties such as glob, directory-files and various
> shell-like procedures (pwd/cd/ls/cp/mv) would go better in a separate
> SRFI or other library. Then the current SRFI would have the essential
> stuff that's hard to implement due to being OS/FFI-dependent. Stuff
> that's simple to implement using portable building blocks from this
> SRFI I would classify as higher-level abstractions.

I can see the argument for that.  On the other hand, with the
exception of call-with-temporary-filename, I think the "simple to
implement using portable building blocks" procedures already
specified and implemented in this SRFI like the other temporary
file procedures are just that, an implementor of the SRFI should
be able to take the simple supplied example code and with little or
no modification add it to their implementation.

That does imply my polishing that code, and having it reviewed by
people who's Scheme experience isn't mostly decades old.  Which
would be a good idea anyway before finalizing this SRFI.

> [ We agree close-directory is required, and implementation details
>   for read-directory, including WinAPI. ]
>
>> (define (directory-fold dir kons knil . o)
>>    (let-optionals o ((dot-files? #f))
>>      (let ((do (open-directory dir dot-files?)))
>>        (let lp ((res knil))
>>          (let ((file (read-directory do)))
>>            (if (not (eof-object? file))
>>                (lp (kons file res))
>>                (begin (close-directory do) res)))))))
>>
>> Which is called with (directory-fold dir cons '() dot-files?) [to
>> implement directory-files]. I have no informed opinion on whether it
>> should be added to the API.
>
> Since you gave very good arguments for exposing an open/read/close API,
> a fold would just be this thin wrapper around it. IMHO we don't need to
> have both APIs. (directory-fold ...) looks like it would be neat to
> implement in terms of a general (resource-fold open read close ...).

Another argument against supplying the ease of use procedures in this
SRFI, just where do we draw the lines?

- Harold