[ OK, Mr. Low-Level takes the chance to re-present his little hTell
  proposal here... ]

I suggest considering the following as a typo/blunder in the Haskell 98
report, for the same reasons as for the changes to the Directory library.
Here it goes:

There are a few design flaws regarding the file positioning functions
in the IO library:

   * Why are there *two* ways to reposition a handle (one via hSeek
     and the other one via hSetPosn)?

   * Why is the handle supposed to be contained in HandlePosn? This
     way one can't keep "a finger" within a file without keeping
     it open. One has to be careful because the file can change in
     the meantime, but this is not under Haskell's control, anyway.

   * If we have a hSeek, where is hTell?

Fixing these points can be done quite easily without breaking any
existing code (with the exception that a new constructor and a new
function are visible now):

   1) Make HandlePosn non-opaque and export it that way:

         data HandlePosn = HandlePosn Handle Integer deriving (Eq,Show)

   2) Add and export

         hTell :: Handle -> IO Integer

      with the "obvious" semantics, see e.g. "man ftell".   :-)

   3) For backwards compatibility keep hGetPosn/hSetPosn, but define
      them via hTell/hSeek:

         hGetPosn :: Handle -> IO HandlePosn
         hGetPosn h = hTell h >>= \p -> return (HandlePosn h p)

         hSetPosn :: HandlePosn -> IO () 
         hSetPosn (HandlePosn h p) = hSeek h AbsoluteSeek p

      Those functions should be removed for H2K.

   4) Perhaps add

         typedef HandlePosition = Integer

      and use it in the code above, but I've got no strong opinion
      about this.

Rationale: Keeping a file position opaque is a bad idea, because
*lots* of file formats rely on the fact that arithmetic is possible
on file positions, see e.g. http://www.wotsit.org/. Merging a file
position with a handle is even worse, because this keeps the file
open even when only the position is needed later.

And a last remark about arithmetic on file positions, as pointed
out by Fergus(Hender)son: One has to be careful about CR/LF vs LF,
but this is not a problem in the real world, ZIP works on M$, too!
:-)

This is all very low-level stuff, but real programs have to deal
with real file formats and the current IO lib makes things extremely
ugly. And another suggestion: Standardize the Socket library in some
way, all sensible operating systems have sockets by now (or the OS
will vanish soon, anyway, due to the omnipresent internetworking).
Hava a look at Java: Even Joe Programmer can write networking code
with a few lines there.

Cheers,
   Sven
-- 
Sven Panne                                        Tel.: +49/89/2178-2235
LMU, Institut fuer Informatik                     FAX : +49/89/2178-2211
LFE Programmier- und Modellierungssprachen              Oettingenstr. 67
mailto:[EMAIL PROTECTED]            D-80538 Muenchen
http://www.informatik.uni-muenchen.de/~Sven.Panne


Reply via email to