Storable tuples and what is 'alignment'?

Alastair Reid alastair at reid-consulting-uk.ltd.uk
Tue Aug 6 17:33:44 EDT 2002


Redirected to ffi at haskell.org

> 1) Why aren't there built in Storable instances for tuples?  Writing
> my own is a pain and in keeping with the 'up to 7tuple' limit, I
> think built in instances would be nice

Probably because we've no idea why you'd want to do it.

The usual deal is that you want to interface to a C struct like this:

  struct pt { int x; int y; };

and want to access it from Haskell so you write something like:

  type Pt = (CInt,CInt)

  instance Storable Pt where
    alignment = #const alignOf(struct pt)
    sizeOf    = #const sizeof(struct pt)
    peek p    = do
                  x <- peek (p + #const(offsetOf(struct pt, x)))
                  y <- peek (p + #const(offsetOf(struct pt, y)))
                  return (x,y)
    poke ...  = ...

That is, for each C type you encounter, you write a custom instance.

An important thing about this is that you're definitely using the
right offset to access the field - there's nothing in the C standard
that requires the obvious relationship between alignment/size and
offset to hold.

So what are you doing that needs Storable instances for arbitrary tuples?

--
Alastair Reid                 alastair at reid-consulting-uk.ltd.uk  
Reid Consulting (UK) Limited  http://www.reid-consulting-uk.ltd.uk/alastair/





More information about the FFI mailing list