Hi folks,<br>I'm trying to make a simple event driven engine. It simply consists of two functions:<br>- "addEvent", where you pass the event name with a callback, <br>- "triggerEvent" where you pass the event name with the data. <br>
the data shall be passed to the callback of the corresponding event.<br><br>I have trouble making it correctly typed.<br>Here is my try:<br><i><br>type Player = Int --dummy types for the example<br>type Rule = Int<br>data EventEnum = NewPlayer | NewRule deriving Eq<br>
data Data = P Player | R Rule<br>data Handler = H (Data -> IO ())<br><br>addEvent :: EventEnum -> Handler -> [(EventEnum, Handler)] -> [(EventEnum, Handler)]<br>addEvent e h es = (e,h):es<br><br>triggerEvent :: EventEnum -> Data -> [(EventEnum, Handler)] -> IO ()<br>
triggerEvent e d es = do<br>         let r = lookup e es<br> case r of <br>                 Nothing -> return ()<br>                 Just (H h) -> h d</i><br><br>The trouble is that I want the user to be only able to add an event that is compatible with its handler:<br>
For example the event NewPlayer should have a handler of type Player -> IO (). The data passed when triggering this event should be only of type Player.<br>How can I do that? It sound like dependant typing...<br><br>Thanks!<br>
Corentin<br>