Socket Options

Sven Panne Sven.Panne at aedion.de
Tue Jun 29 10:23:38 EDT 2004


Carsten Schultz wrote:
>>Wouldn't that make
>>  getSocketOption :: Socket -> SocketOption -> IO Int
>>a bit strange? How would you propose to change it?
> 
> Possible, but also possibly overkill, would be:
> 
> newtype Debug = Debug Bool
> newtype SendBuffer = SendBuffer (Maybe Int)
> [...]
> 
> class SocketOption a where [...]
> 
> instance SocketOption Debug [...]
> instance SocketOption SendBuffer [...]
> [...]
> 
> setSocketOption :: SocketOption a => Socket -> a -> IO ()
> getSocketOption :: SockerOption a => Socket -> IO a
> 
> 
> However, while quite clever :-), this would not be far from having a
> seperate get/set-functions for every option (and could indeed be
> implemented that way).

This is a recurring problem and has been solved several times e.g. in some
Haskell GUI bindings and my OpenGL/GLUT binding, see e.g.:

    http://haskell.org/HOpenGL/newAPI/OpenGL/Graphics.Rendering.OpenGL.GL.StateVar.html

If we had:

    socketOptionDebug :: Socket -> StateVar Bool
    socketOptionSendBuffer :: Socket -> StateVar (Maybe Int)
    ...

then we could write:

    socketOptionDebug mySocket $= True
    maybeBuf <- get $ socketOptionSendBuffer mySocket
    ...

But there are some reasons for not using this here:

    * Alas, HasGetter/HasSetter/StateVar/... are not standard.

    * It's a bit overkill for the simple task at hand. :-)

I would simply go for a single setter function and several getters here.

Cheers,
    S.



More information about the Glasgow-haskell-users mailing list