<div dir="ltr"><div class="gmail_extra">The Java type system is a lot less rich. You can take an object and call methods on it, knowing only that it implements an interface. In Haskell, you can do so much more. You don&#39;t even need an implementation, you can rely on the type of the binding instead:</div>


<div class="gmail_extra"><br></div><div class="gmail_extra">extremes :: Bounded b =&gt; Bool -&gt; b</div><div class="gmail_extra">extremes lower = if lower then minBound else maxBound</div><div class="gmail_extra">
-- e.g.</div><div class="gmail_extra">extremes True :: Int -- -9223372036854775808</div><div class="gmail_extra">data Foo = A | B | C | D deriving (Bounded, Show)</div><div class="gmail_extra">extremes False :: Foo -- D</div>

<div class="gmail_extra">
<br></div><div class="gmail_extra">Or (with a few language extensions), you can have the typeclass take multiple arguments. In Java, you can&#39;t do anything like this:</div><div class="gmail_extra"><br></div><div class="gmail_extra">

class Convert a b where</div><div class="gmail_extra">   convert :: a -&gt; b</div><div class="gmail_extra"><br></div><div class="gmail_extra">instance Show a =&gt; Convert a String where</div><div class="gmail_extra">   convert = show</div>

<div class="gmail_extra"><br></div><div class="gmail_extra" style>instance Convert Foo String where</div><div class="gmail_extra" style>   convert A = &quot;A&quot;</div><div class="gmail_extra" style>   convert B = &quot;B&quot;</div>

   convert C = &quot;C&quot;<br class="">   convert D = &quot;D&quot;<div class="gmail_extra" style>  </div><div class="gmail_extra" style><br></div><div class="gmail_extra" style>Peter</div><div class="gmail_extra"><br>

<br><div class="gmail_quote">On 31 January 2013 23:36, Mateusz Kowalczyk <span dir="ltr">&lt;<a href="mailto:fuuzetsu@fuuzetsu.co.uk" target="_blank">fuuzetsu@fuuzetsu.co.uk</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


Greetings,<br>
<br>
I often wonder how one would explain type classes to someone coming from<br>
an environment such as Java. Whenever I think about type classes, I seem<br>
to think of them as Java interfaces. Bah, even the bottom of [1] states<br>
&gt; Haskell classes are roughly similar to a Java interface. Like an<br>
interface declaration, a Haskell class declaration defines a protocol<br>
for using an object rather than defining an object itself.<br>
<br>
Is there more to this `roughly similar&#39; statement? Syntax is an obvious<br>
difference but beyond that, I can&#39;t think of anything I can do with a<br>
Haskell type class that I wouldn&#39;t be able to do with similar amount of<br>
effort with a Java interface, except for the fact that the interface<br>
would look absolutely disgusting syntax wise.<br>
<br>
Any insight appreciated.<br>
<br>
<br>
[1] - <a href="http://www.haskell.org/tutorial/classes.html" target="_blank">http://www.haskell.org/tutorial/classes.html</a><br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div></div>