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's bad to try to unbox an existential type using a cast. OK, but without I really can'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) => 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 "EventData") []<br>
<br>data EventHandler = forall e . (Event e) => EH e (EventData e -> IO ())<br><br>addEvent :: (Event e) => e -> (EventData e -> IO ()) -> [EventHandler] -> [EventHandler] <br>addEvent e h ehs = (EH e h):ehs<br>
<br>triggerEvent :: (Event e) => e -> (EventData e) -> [EventHandler] -> IO ()<br>triggerEvent e d ehs = do<br> let r = find (\(EH myEvent _) -> cast e == Just myEvent) ehs<br> case r of<br> Nothing -> return ()<br>
Just (EH _ h) -> case cast h of<br> Just castedH -> castedH d<br> Nothing -> 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 -> IO ()) -> (EventData e) -> IO ()<br>apply = ($)</i><br><br><br>Thanks!<br>Corentin<br>