[m-dev.] Socket Library

Paul Bone pbone at csse.unimelb.edu.au
Wed Oct 20 09:50:15 AEDT 2010


On Wed, Oct 20, 2010 at 08:39:32AM +1100, Peter Ross wrote:
> On Wed, Oct 20, 2010 at 3:04 AM, Daniel Waterworth
> <da.waterworth at gmail.com> wrote:
> > Hi,
> >
> > I'm making a socket library in mercury. It works (I have sent simple
> > HTTP requests using the code I've written and it supports IPv6), but
> > as it stands it doesn't check for errors. So, how should C code go
> > about raising exceptions? I haven't found any documentation on the
> > subject.
> >
> If I was writing a socket library, I would make sure that it
> implements the stream interface from the standard library.  In that
> case errors are raised by constructing a mercury type which indicates
> the success or failure of the operation.
> 
> Generally I write code like the following to handle that case.  First
> you write a low level predicate whose body is written in C.  That
> predicate returns an integer indicating whether or not the operation
> succeeded and returns a string which is the reason why it failed.
> 
> :- pred send_string_c(string::in, int::out, string::out, io::di, io::uo) is det.
> 
> The you write you high level interface.
> 
> :- pred send_string(string::in, result::out, io::di, io::uo) is det.
> 
> send_string(S, Result, !IO) :-
>   send_string_c(S, Res, Msg, !IO),
>   ( Res = -1 ->
>     Result = error(Msg)
>   ;
>     Result = ok
>   ).

I also recommend this.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/developers/attachments/20101020/31022b32/attachment.sig>


More information about the developers mailing list