
[See comments at bottom] Bulat Ziganshin wrote:
Finally i've implemented the following (you then would use 'withForeignPtr' to work with contents of ForeignPtr):
-- ----------------------------------------------------------------------------- -- Encode/decode contents of memory buffer
encodePtr :: (Binary a, Integral size) => a -> IO (ForeignPtr x, size) encodePtrLE :: (Binary a, Integral size) => a -> IO (ForeignPtr x, size) encodePtrBitAligned :: (Binary a, Integral size) => a -> IO (ForeignPtr x, size) encodePtrBitAlignedLE :: (Binary a, Integral size) => a -> IO (ForeignPtr x, size) encodePtr = encodePtr' openByteAligned encodePtrLE = encodePtr' openByteAlignedLE encodePtrBitAligned = encodePtr' openBitAligned encodePtrBitAlignedLE = encodePtr' openBitAlignedLE
decodePtr :: (Binary a, Integral size) => Ptr x -> size -> IO a decodePtrLE :: (Binary a, Integral size) => Ptr x -> size -> IO a decodePtrBitAligned :: (Binary a, Integral size) => Ptr x -> size -> IO a decodePtrBitAlignedLE :: (Binary a, Integral size) => Ptr x -> size -> IO a
decodePtr = decodePtr' openByteAligned decodePtrLE = decodePtr' openByteAlignedLE decodePtrBitAligned = decodePtr' openBitAligned decodePtrBitAlignedLE = decodePtr' openBitAlignedLE
Am I the only one who finds this encoding-of-types in the _name_ of a function quite distateful? There is no type safety being enforced here, no ensuring one will not be encoding a Ptr one way and decoding it another. Why not use Haskell's type system to help you there? One could imagine putting encodePtr' and decodePtr' in a type class for example? Or many other solutions. This is not meant to be a general critique of this habit of encoding types into function names, not of the particular instance above. My interest in starting this thread is to discuss the solutions that work, and the situations where no solution currently seems to exist. I believe there may be instances of encoding-types-in-names that are currently necessary in Haskell because the type system is not powerful enough to do anything else. Using Typeable and a type-witness just moves the problem, it does not ``solve'' it. Jacques