Proposal: Don't require users to use undefined
Bas van Dijk
v.dijk.bas at gmail.com
Tue Oct 26 14:36:19 EDT 2010
On Tue, Oct 26, 2010 at 8:06 PM, Roman Leshchinskiy <rl at cse.unsw.edu.au> wrote:
> 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
More information about the Libraries
mailing list