<div dir="ltr">Sorry for adding to the dogpile, should've guessed others would point to the same resource :)</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 19, 2015 at 11:11 AM, Christopher Allen <span dir="ltr"><<a href="mailto:cma@bitemyapp.com" target="_blank">cma@bitemyapp.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">This is a pattern people usually refer to as the "existential typeclass antipattern".<div><br></div><div>An article goes into more depth here: <a href="https://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/" target="_blank">https://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/</a></div><div><br></div><div>Sometimes you need a GADT with an existential type and a typeclass, but it doesn't occur as often as the equivalent does in OO.</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Fri, Jun 19, 2015 at 10:48 AM, Nicholls, Mark <span dir="ltr"><<a href="mailto:nicholls.mark@vimn.com" target="_blank">nicholls.mark@vimn.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">





<div lang="EN-GB" link="blue" vlink="purple">
<div>
<p class="MsoNormal">My initial guess would be no…its built into the language<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">But….<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I find myself tempted to use it (in order to match on a type that’s the instance of a typeclass)…which makes me think I’ve missed something.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">So…<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">> data Foo = Foo<u></u><u></u></p>
<p class="MsoNormal">> data Bar = Bar<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Ooo…this looks like an OO design pattern…surely wrong?<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">> class Wibble a where<u></u><u></u></p>
<p class="MsoNormal">>   visit :: (Foo -> b) -> (Bar -> b) -> a -> b<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">> instance Wibble Foo where<u></u><u></u></p>
<p class="MsoNormal">>   visit f _ x = f x<u></u><u></u></p>
<p class="MsoNormal">> instance Wibble Bar where<u></u><u></u></p>
<p class="MsoNormal">>   visit _ f x = f x<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I want a list of Wibbles....<u></u><u></u></p>
<p class="MsoNormal">hmmm...<u></u><u></u></p>
<p class="MsoNormal">(Wibble a) => [a] is clearly wrong...<u></u><u></u></p>
<p class="MsoNormal">I want [(Wibble a) =>a]<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">so I package it up?<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">> data WibblePackage where<u></u><u></u></p>
<p class="MsoNormal">>   WibblePackage :: (Wibble a) => a -> WibblePackage<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">lets try this now…so pointless function across Wibbles in a list.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">> fizzBuzz :: [WibblePackage] -> Integer<u></u><u></u></p>
<p class="MsoNormal">> fizzBuzz []       = 0<u></u><u></u></p>
<p class="MsoNormal">> fizzBuzz ((WibblePackage x) : xs) = (visit (\_ -> 1) (\_ -> 2) x) + (fizzBuzz xs)<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">> help :: Integer<u></u><u></u></p>
<p class="MsoNormal">> help = fizzBuzz [WibblePackage Foo,WibblePackage Bar]<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">That works!<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">OO nerds will get uptight about “closing” the Wibble typeclass….but I’ve managed to create a list of different types and have a mechanism for recovering the type.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">How would a Haskell nerd do this? (I have looked, but they look more complicated….maybe to my OO eye).<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p><br><br>CONFIDENTIALITY NOTICE<br><br>This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.<br><br>While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.<br><br>Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.<br><br>MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe.  MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc.  Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.<br></p></div>

<br></div></div><span class="">_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
<br></span></blockquote></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr">Chris Allen<br><div><span style="font-size:12.8000001907349px">Currently working on </span><a href="http://haskellbook.com" target="_blank">http://haskellbook.com</a></div></div></div></div></div></div>
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr">Chris Allen<br><div><span style="font-size:12.8000001907349px">Currently working on </span><a href="http://haskellbook.com" target="_blank">http://haskellbook.com</a></div></div></div></div></div></div>
</div>