[m-users.] Bug in posix.readdir (fixed)

Volker Wysk post at volker-wysk.de
Mon Oct 14 12:58:18 AEDT 2019


Am Montag, den 14.10.2019, 10:25 +1100 schrieb Julien Fischer:
> Hi Volker,
> 
> On Fri, 11 Oct 2019, Volker Wysk wrote:
> 
> > The readdir/4 predicate from the posix library is buggy. It returns
> > an
> > "unknown error code" error for the end of the directory stream.
> > 
> > 
> > I've fixed it. I've changed the type from
> > 
> > :- pred readdir(dir::in,
> > 	        posix.result(string)::out,
> > 		io::di, io::uo) is det.
> > 
> > to
> > 
> > :- pred readdir(dir::in,
> >                posix.result(maybe(string))::out,
> >                io::di, io::uo) is det.
> > 
> > 
> > The new version returns "posix.ok(yes(File))" for a directory
> > entry,
> > and "posix.ok(no)" for the end of the stream.
> 
> I don't like returning a maybe type like that, it means you need
> two levels of switching at call sites for readdir/4.  I suggest
> adding the type:
> 
>     :- type readdir_result
>          --->	ok(string)
>          ;       eof
>          ;       error(posix.error).
> 
> 
> and using that.

Not sure if I understand correctly, but wouldn't it just be one level
of switching:

    readdir(Dir, Res, !IO),
    (
        Res = ok(yes(File)),
	...
    ;
	Res = ok(no),
	...
    ; 
	Res = error(Err),
	...
    )


Cheers,
Volker



More information about the users mailing list