[m-dev.] Socket Library
Peter Ross
pro at missioncriticalit.com
Wed Oct 20 12:03:33 AEDT 2010
On Wed, Oct 20, 2010 at 11:16 AM, Julien Fischer
<juliensf at csse.unimelb.edu.au> wrote:
>
> On Wed, 20 Oct 2010, Paul Bone wrote:
>
>> 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 would recommend using an exported enumeration to return the result
> status. Passing small integers back and forth over the foreign language
> interface tends to lead to less maintainable code IMO.
>
I agree with that point, exported enumerations are a nice way to do
that. They didn't exist when I wrote most of my interfaces.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions: mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the developers
mailing list