Hi folks,<br>I&#39;m trying to make a simple event driven engine. It simply consists of two functions:<br>- &quot;addEvent&quot;, where you pass the event name with a callback, <br>- &quot;triggerEvent&quot; 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 -&gt; IO ())<br><br>addEvent :: EventEnum -&gt; Handler -&gt; [(EventEnum, Handler)] -&gt; [(EventEnum, Handler)]<br>addEvent e h es = (e,h):es<br><br>triggerEvent :: EventEnum -&gt; Data -&gt; [(EventEnum, Handler)] -&gt; IO ()<br>
triggerEvent e d es = do<br>           let r = lookup e es<br>    case r of <br>                      Nothing -&gt; return ()<br>                      Just (H h) -&gt; 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 -&gt; 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>