Hi all,<br>I read somewhere (here: <a href="http://stackoverflow.com/questions/2300275/how-to-unpack-a-haskell-existential-type">http://stackoverflow.com/questions/2300275/how-to-unpack-a-haskell-existential-type</a>) that it&#39;s bad to try to unbox an existential type using a cast. OK, but without I really can&#39;t figure out how to do what I want:<br>
<br><i>data NewPlayer = NewPlayer deriving (Typeable, Eq)<br>data NewRule = NewRule deriving (Typeable, Eq)<br><br>class (Eq e, Typeable e) =&gt; Event e where<br>    data EventData e<br><br>instance Event NewPlayer where<br>
    data EventData NewPlayer = P Int<br><br>instance Event NewRule where<br>    data EventData NewRule = R Int<br><br>instance Typeable1 EventData where <br>    typeOf1 _ = mkTyConApp (mkTyCon &quot;EventData&quot;) []<br>
<br>data EventHandler = forall e . (Event e) =&gt; EH e (EventData e -&gt; IO ())<br><br>addEvent :: (Event e) =&gt; e -&gt; (EventData e -&gt; IO ()) -&gt; [EventHandler] -&gt; [EventHandler] <br>addEvent e h ehs = (EH e h):ehs<br>
<br>triggerEvent :: (Event e) =&gt; e -&gt; (EventData e) -&gt; [EventHandler] -&gt; IO ()<br>triggerEvent e d ehs = do<br>    let r = find (\(EH myEvent _) -&gt; cast e == Just myEvent) ehs<br>    case r of<br>       Nothing -&gt; return ()<br>
       Just (EH _ h) -&gt; case cast h of<br>        Just castedH -&gt; castedH d<br>        Nothing -&gt; return ()</i><br><br>How to remove the casts from triggerEvent? All that I want is to apply the handler found on the data passed in parameter.<br>
I tried to add a function apply in the class, without success:<br><i>apply :: (EventData e -&gt; IO ()) -&gt; (EventData e) -&gt; IO ()<br>apply = ($)</i><br><br><br>Thanks!<br>Corentin<br>