<br><br><div class="gmail_quote">On Thu, Jun 14, 2012 at 2:04 PM, Corentin Dupont <span dir="ltr">&lt;<a href="mailto:corentin.dupont@gmail.com" target="_blank">corentin.dupont@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
That look really nice!<br>Unfortunately I need to have an heterogeneous list of all events with their handlers.<br>With this test code it won&#39;t compile:<br><br>test1 = addEvent (New :: Event Player) (H (undefined::(Player -&gt; IO ()))) []<br>

test2 = addEvent (New :: Event Rule) (H (undefined::(Rule -&gt; IO ()))) test1<div class="HOEnZb"><div class="h5"><br></div></div></blockquote><div><br></div><div>Right, okay.  Heterogenous lists are tricky, but I think we can get away with using ExistentialQuantification, since you seem to only want to dispatch over the heterogenous types.  The assumption I made is a big deal!  It means you can&#39;t extract the d value.  You can only apply properly typed functions (your handlers) on it.</div>
<div> </div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><i><div style="text-align:left;display:inline!important">{-# LANGUAGE ExistentialQuantification #-} </div></i><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
</blockquote><i><div style="display:inline!important"><div style="text-align:left;display:inline!important"><i><div style="display:inline!important">type Player = Int</div></i></div></div></i><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
</blockquote><i><div style="display:inline!important"><div style="text-align:left;display:inline!important"><i><div style="display:inline!important">type Rule = Int</div></i></div></div></i><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
</blockquote><i><div style="text-align:left;display:inline!important"><i>data Event d = New d</i></div></i><br><i><br></i><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><i style="text-align:left">class Handled data where -- Together with EventHandler, corresponds to your &quot;Data&quot; type</i><br>
<i><br></i><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><i style="text-align:left">data EventHandler = forall d . (Handled d) =&gt; EH (Event d) (d -&gt; IO ()) -- EventHandler takes the place of your (Event d, Handler d) pairs without referring to d.</i><br>
<i><br></i><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><i style="text-align:left">instance Handled Player</i><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><i style="text-align:left">instance Handled Rule</i><br>
<div style="text-align:left"><br></div><div style="text-align:left"><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><i style="text-align:left">addEvent :: (Handled d) =&gt; Event d -&gt; Handler d -&gt; [EventHandler] -&gt; [EventHandler] -- Every [EventHandler] made using addEvent will be of &quot;correct&quot; types (i.e., preserve the typing invariants you want), but YOU must ensure that only [EventHandler]s made in this way are used.  This can be done statically with another type and an explicit export list.  We can talk about that later, if this works in principle.</i><br>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><span style="text-align:left"> </span><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><span style="text-align:left"> </span><br>
<div class="gmail_quote"><div class="HOEnZb"><div class="h5"><div class="gmail_quote"><div class="gmail_quote"><div style="text-align:left"><i>triggerEvent :: (Handled d) =&gt; Event d -&gt; d -&gt; [EventHandler] -&gt; IO ()</i></div>
</div></div></div></div></div><div class="gmail_quote"><div> </div></div><br>