[Haskell-cafe] Writing a Storable instance for a C union

Nikita Karetnikov nikita at karetnikov.org
Sat Dec 14 16:46:59 UTC 2013


> It's as simple as :

> peek p = do
> 	v0 <- peekByteOff p 0
> 	case v0 of
> 		1 -> MyStruct <$> peekByteOff p 4
> 		2 -> FooStruct <$> peek...

> But this function can only have a single type, hence my suggestion
> that you should create a union type to represent the C union :

> type SomeUnion = MyStruct Byte | FooStruct Integer | ...

Yes, but I also want to poke, so SomeUnion must be an instance of
Storable.  Could you show how to define it properly or provide a
workaround?

I’m attaching some code, which is in the public domain, to demonstrate
the problem.  It was generated using c2hsc and hsc2hs.  The lines marked
with XXX were either modified or added by me.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Test.hs
Type: text/x-haskell
Size: 2766 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131214/0aba77d6/attachment.hs>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131214/0aba77d6/attachment.sig>


More information about the Haskell-Cafe mailing list