[Haskell-cafe] Nice addition to Foreign: castAny
Maurício CA
mauricio.antunes at gmail.com
Mon Oct 26 22:48:32 EDT 2009
This could be beside castPtr, castCharToCChar etc.
----
castAny :: (Storable a, Storable b) => a -> b
castAny = unsafePerformIO . genericCast
where
genericCast :: (Storable a, Storable b) => a -> IO b
genericCast v = return undefined >>= \r ->
allocaBytes (max (sizeOf v) (sizeOf r)) $ \p ->
poke p v >> if False then return r else peek (castPtr p)
----
GHCi:
> let a = -1000 :: Int16
> castAny a :: Word16 -->
64536
> castAny a :: Ptr ()
0xb4c2fc18
> castAny (castAny a :: Ptr ()) :: Int16
-1000
> let b = pi :: CLDouble
> b
3.141592653589793
> castAny b :: CInt
1413754136
> castAny b :: Ptr ()
0x54442d18
> castAny b :: CFloat
3.3702806e12
> castAny b :: Int8
24
At minimum, this is safer than 'unsafeCoerce'. What do you think?
Best,
Maurício
More information about the Haskell-Cafe
mailing list