<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'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 => Bool -> 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'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 -> b</div><div class="gmail_extra"><br></div><div class="gmail_extra">instance Show a => 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 = "A"</div><div class="gmail_extra" style> convert B = "B"</div>
convert C = "C"<br class=""> convert D = "D"<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"><<a href="mailto:fuuzetsu@fuuzetsu.co.uk" target="_blank">fuuzetsu@fuuzetsu.co.uk</a>></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>
> 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' statement? Syntax is an obvious<br>
difference but beyond that, I can't think of anything I can do with a<br>
Haskell type class that I wouldn'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>