
Duncan Coutts wrote:
On Sat, 2007-06-02 at 18:31 +0200, Björn Bringert wrote:
I'll just keep spamming this list with features I'm missing. I use #pointer to create newtypes, but I'm missing Storable instances for the produced newtypes. It's very nice to be able to use alloca, peek etc for passing pointers around.
Even GHC can't derive Storable since the type is recursive, but it's easy to write the instance. Here's what I do now:
{#pointer *SWIrecRecognizer as RecRecognizer newtype #}
instance Storable RecRecognizer where sizeOf (RecRecognizer r) = sizeOf r alignment (RecRecognizer r) = alignment r peek p = fmap RecRecognizer (peek (castPtr p)) poke p (RecRecognizer r) = poke (castPtr p) r
c2hs could easily produce that Storable instance, and I can't see any reason not to.
So this gives it a storable instance that interprets the newtype as a pointer, but not as the thing pointed to? (which would be the usual interpretation right?) I'm not sure I understand why you would want this, and even if you did it'd prevent people doing the more normal Storable instance for the thing pointed to. I'm clearly missing something here.
My main motivation for this is to be able to work with out-parameters which return these pointers. For example, I want to use a function like this one: int SWIrecRecognizerCreate (SWIrecRecognizer **rec); This function returns a pointer to the abstract type SWIrecRecognizer, by using an out-parameter. I want to be able to use this #fun declaration for it: {#fun recRecognizerCreate { alloca- `RecRecognizer' peek*} -> `Int' #} Without the Storable instance, I would have to do something like this (not tested): {#fun recRecognizerCreate { allocaRecRecognizer- `RecRecognizer' peekRecRecognizer*} -> `Int' #} where allocaRecRecognizer = allocaBytes {#sizeof RecRecognizer} peekRecRecognizer = liftM (RecRecognizer . castPtr) . peek My interpretation was that #pointer is used for abstract types whose internals you don't mess with from Haskell, but just pass around. Thus, the only thing you want to do with them is to store and retrieve the pointers, not the pointed-to objects. Maybe that's not always true, and in that case, producing a Storable instance like the above is wrong. It would be nice to have the option to do it though. /Björn