Re: Marshalling C unions

Henning Thielemann <[email protected]> Wed, 13 Apr 2011 21:29:18 +0200 (CEST)
Newsgroups gmane.comp.lang.haskell.ffi
Message-ID <alpine.DEB.2.00.1104132127060.6682@anubis>
On Wed, 13 Apr 2011, Nabil Alsharif wrote:

> instance Storable myUnion where
>    size _ = #{size my_union} -- <-- hsc2hs shortcut
>    alignment _ = alignment undefined::CInt -- <-- What should this really 
> be?
>    peek ptr = do -- <-- How are you supposed to know which element to extract?

That's the great "feature" of C unions, that you cannot know that in 
general. You have to store somewhere else, in a boolean or flag set, 
whether the stored union value contains a 'char' or an 'int'.

>    poke ptr (I i) =  poke ptr i -- <-- Or should this be #{poke my_union, i}  ptr i ?
>    poke ptr (C c) = poke ptr c