Haskell and C—functions returning more than one value

I’ve finally found some time to play with c2hs again. This time I had a quick look at passing pointers to C functions. Here’s what I found, in a rather short format:

{# fun foo { alloca- `Int' peekIntConv* } -> `()' #} => foo :: IO Int
{# fun foo { `Int' peekIntConv* } -> `()' #} => foo :: Int -> IO Int

That is, the former is an out argumenti while the latter is an inout argument.

A C function like this:

int foo(int *);

which is defined like this in haskell

{# fun foo { `Int' peekIntConv* } -> `Int' #}

will return a tuple of two integers, the first one is the returned value from foo the other is the inout argument to foo.

This means that it’s fairly simple to deal with APIs that follow the convention that functions return an error code and that the last argument is the actual result of the function.

  1. It seems it always receives a pointer to an integer instance containing the value 0.[back]
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>