Marshalling C unions

Henning Thielemann lemming at
Wed Apr 13 21:29:18 CEST 2011

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

More information about the FFI mailing list