[Haskell-cafe] Making MVar and Chan Instances of Typeable

Benjamin Franksen benjamin.franksen at bessy.de
Fri Nov 5 09:35:06 EST 2004


On Friday 05 November 2004 15:07, Benjamin Franksen wrote:
> instance Typeable a => Typeable (MVar a) where
>   typeOf x =
>     mkAppTy (mkTyCon "Control.Concurrent.MVar.MVar") [typeOf y]
>     where
>       y = unsafePerformIO $ do
>         z <- newEmptyMVar >>= readMVar
>         return (z `asTypeOf` x)

which is wrong because it also passes the typeOf of the MVar and not the 
content. This one is correct, I hope:

instance Typeable a => Typeable (MVar a) where
  typeOf x =
    mkAppTy (mkTyCon "Control.Concurrent.MVar.MVar") [typeOf v]
    where
      v = unsafePerformIO $ do
        y <- newEmptyMVar
        readMVar (y `asTypeOf` x)

On Friday 05 November 2004 16:44, Koji Nakahara wrote:
> instance Typeable a => Typeable (MVar a) where
> typeOf (x :: MVar a) =
> 	mkAppTy (mkTyCon "Control.Concurrent.MVar.MVar") [typeOf (undefined::a)]

Yes, that's it. The above is a lot more convoluted but has a small advantage: 
it doesn't need -fglasgow-exts.

I understand now, why pattern signatures were deemed a useful feature!

Thanks to all who helped.

Cheers,
Ben


More information about the Haskell-Cafe mailing list