
On Tue, Oct 26, 2010 at 8:06 PM, Roman Leshchinskiy
On 26/10/2010, at 17:15, Bas van Dijk wrote:
malloc :: Storable a => IO (Ptr a) malloc = doMalloc undefined where doMalloc :: Storable b => b -> IO (Ptr b) doMalloc dummy = mallocBytes (sizeOf dummy)
I find the use of 'undefined' ugly;
How about:
malloc :: Storable a => IO (Ptr a) malloc = doMalloc Nothing where doMalloc :: Storable b => Maybe b -> IO (Ptr b) doMalloc dummy = mallocBytes (sizeOf dummy)
I don't think this will work because 'Maybe a' doesn't have a Storable instance.
or perhaps
data Dummy a = Dummy
malloc :: Storable a => IO (Ptr a) malloc = doMalloc Dummy where doMalloc :: Storable b => Dummy b -> IO (Ptr b) doMalloc dummy = mallocBytes (sizeOf dummy)
Again I think this won't work because you need to add a Storable instance like: instance Storable a => Storable (Dummy a) where sizeOf _ = sizeOf (undefined :: a) peek = ... poke = ... ... Regards, Bas