Q: Bug? 'Storable' and 'with'

Jan Kybic kybic@ieee.org
14 Oct 2002 15:42:17 +0200


Hi,
        I am using ghc-5.04 and a code like:

with c ( \c' -> hPutBuf h c' (sizeOf c))

fails with "Fail: Prelude.undefined" when "c" is a user defined type,
such as a pair:

instance (Storable at,Storable bt) => Storable (at,bt) where
	 sizeOf (a,b) = sizeOf a + sizeOf b
	 alignment (a,b) = max (alignment a) (alignment b)
	 peek p = do a <- peek ((castPtr p):: Ptr at) 
		     b <- peekByteOff ((castPtr p):: Ptr bt) (sizeOf a)
		     return (a,b)
	 poke p (a,b) = do poke ((castPtr p):: Ptr at) a
			   pokeByteOff ((castPtr p):: Ptr bt) (sizeOf a) b


On the other hand, if I replace 'with' with 'with1':

with1 :: (Storable a) => a -> ( Ptr a -> IO b ) -> IO b
with1 x f = do p <- mallocBytes (sizeOf x)
	       let p'=castPtr p :: Ptr a
	       poke p' x
	       y <- f p'
	       free p
	       return y

it seems to work. I could not even get to work 'malloc :: IO (Ptr
(Int,Int))', while 'malloc :: IO (Ptr Int)' works fine.
Am I doing something wrong, or is there a bug in
the library code?

Yours,

Jan


-- 
-------------------------------------------------------------------------
Jan Kybic <kybic@ieee.org>      Odyssee, INRIA, Sophia-Antipolis, France
       or <Jan.Kybic@sophia.inria.fr>,tel. work +33 492 38 7589, fax 7845
             http://www-sop.inria.fr/odyssee/team/Jan.Kybic/index.en.html