Getting the file descriptor from a handle, without closing it
Bas van Dijk
v.dijk.bas at gmail.com
Sat Oct 1 10:38:33 CEST 2011
On 1 October 2011 08:30, Volker Wysk <pf3 at volker-wysk.de> wrote:
> 1.
>
> data FD = FD {
> fdFD :: {-# UNPACK #-} !CInt,
> fdIsNonBlocking :: {-# UNPACK #-} !Int
> }
>
> What is that exclamation mark?
That's a strictness annotation and is haskell98/2010:
http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-680004.2
And that "{-# UNPACK #-}"?
To quote:
http://www.haskell.org/ghc/docs/latest/html/users_guide/pragmas.html#unpack-pragma
"The UNPACK indicates to the compiler that it should unpack the
contents of a constructor field into the constructor itself, removing
a level of indirection"
> 2.
>
> handleToFd' :: Handle -> Handle__ -> IO (Handle__, Fd)
> handleToFd' h h_ at Handle__{haType=_,..} = do
> case cast haDevice of
> -- ...
>
> haDevice should be a function. How could you cast it?
Note the .. in the record pattern. That is a language extension called
RecordWildCards. It replaces each elided field f by the pattern f = f:
http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#record-wildcards
> 3.
>
> data Handle__
> = forall dev enc_state dec_state .
> (IODevice dev, BufferedIO dev, Typeable dev) =>
> Handle__ {
> -- ...
> }
> deriving Typeable
>
> What's that "forall" thing?
Handle__ is an existentially quantified data constructors:
http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#existential-quantification
> 4.
>
> handleToFd' h h_ at Handle__{haType=_,..} = do
> case cast haDevice of
> Nothing -> -- ...
> Just fd -> do
> -- ...
> return (Handle__{haType=ClosedHandle,..},
> Fd (fromIntegral (FD.fdFD fd)))
>
> What's this ".." inside "Handle__{haType=ClosedHandle,..}"?
See answer of 2
Regards,
Bas
More information about the Glasgow-haskell-users
mailing list