On Wed, Mar 30, 2011 at 5:27 PM, Yves Parès <limestr...@gmail.com> wrote: > Hello, > > To access a haskell type from C code, do we have to make this type instance > of Storable (and so to also define an homomorph structure on the C side), or > does FFI specifies some C function to enable C code to access the fields, or > even some GHC-specific functions? (Even if I'd prefer a portable way) > > I mean, if I have the type: > > data Character = Character { > pos :: (Float, Float), > size :: Float, > sprite :: Image > } > > Do I compulsorily have to double its definition on C side: > > stuct CCharacter { > float x; > float y; > float size; > Image sprite; > } > > and then make Character instance of Storable, which will make the program > *copy* Characters to CCharacters each time that I must call a C function to, > say, draw a character? >
I haven't needed to access Haskell data from C, but the way I see it you have two choices: * Write a Storable instance for your Haskell type, as you said. Be sure to use the Haskell CTypes as intermediaries so you don't go crazy on the other side. * StablePtr magic and foreign-exported wrapper functions to do all of your data-manipulation in Haskell. There's less duplication this way, but perhaps more boiler-plate. Plus you need to manage the lifetime of the StablePtr. I don't know of anything else off-hand. Most people focus on going the other way (using C types in Haskell). Antoine > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > > _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe