Proposal: Don't require users to use undefined

Bas van Dijk v.dijk.bas at gmail.com
Wed Oct 27 03:19:12 EDT 2010


On Tue, Oct 26, 2010 at 9:36 PM, Roman Leshchinskiy <rl at cse.unsw.edu.au> wrote:
> This is what I was thinking while typing the above nonsense. You could redefine sizeOf and friends to take (Dummy a) instead of `a' as an argument and then use this:
>
> 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)

Yes, this is another possibility. It doesn't require undefined but
does require a dummy argument which is only used to help the type
checker.

> In any case, for me this is not a compelling reason to break several widely used interfaces.

Fair enough. However I find the following much easier to read (no
auxiliary function and no dummy argument):

malloc :: forall a. Storable a => IO (Ptr a)
malloc = mallocBytes (untag (sizeOf :: SizeOf a))

Bas


More information about the Libraries mailing list