
On Tue, 26 Oct 2010, Bas van Dijk wrote:
malloc :: forall a. Storable a => IO (Ptr a) malloc = mallocBytes (untag (sizeOf :: SizeOf a))
(Note that this does require the ScopedTypeVariables language extension.)
Haskell 98 solution would be nicer. Something like
malloc :: Storable a => IO (Ptr a) malloc = let aux :: Storable a => SizeOf a -> IO (Ptr a) aux = mallocBytes . untag in aux sizeOf
There's a possible 4th proposal that I'm thinking about: The Bits class from Data.Bits has the bitSize :: a -> Int method. Maybe it would be a good idea to replace that as well with: bitSize :: BitSize a; type BitSize a = Tagged a Int However I think bitSize is more often applied to an actual value than to 'undefined' so I need to investigate that a little further.
RealFloat class also provides a lot of examples that could be adapted to the Tagged scheme. Since all of the affected modules are quite basic, this would break a lot of code. Thus I would propose a less invasive path: 1. Add functions that provide the Tagged interface and implement them using 'sizeOf' and 'alignment'. 2. Add functions that convert a sizeOfTagged and alignmentTagged function to 'sizeOf' and 'alignment' in order to allow Storable instance declaration, that not need to cope with undefined. 3. Document how to use the Tagged types with Haskell 98. 4. In the future you may deprecate the immediate use of 'sizeOf' and 'alignment'.