[Haskell-cafe] Portable implementation of unsafeCoerce

John Meacham john at repetae.net
Thu Feb 8 18:51:07 EST 2007


On Tue, Feb 06, 2007 at 04:58:28PM +0100, Alfonso Acosta wrote:
> Hi all,
> 
> Reading the sources of Lava (a Haskell Hardware Description DSL) I run
> into this definition ...
> 
> 
> unsafeCoerce :: a -> b
> unsafeCoerce a = unsafePerformIO $
>  do writeIORef ref a
>     readIORef ref
> where
>  ref = unsafePerformIO $
>    do newIORef undefined
> 
> Is this actually equivalent to the infamous unsafeCoerce# ?

Certainly not in jhc. However, In jhc one can use unsafeCoerce__ which
is provided in Jhc.Prim. An important restriction on using it in jhc
that might not be obvious is that you must not 'seq' a value that has
been coerced to another type.
        
        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list