[Haskell-beginners] Real world example of Typeclasses > Interfaces
alecbenzer at gmail.com
Sat Sep 4 13:04:43 EDT 2010
On Sat, Sep 4, 2010 at 12:29 PM, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
> On Fri, Sep 03, 2010 at 10:51:59PM -0400, Alec Benzer wrote:
>> But there's no real way of ensuring an argument implements two interfaces,
>> whereas in Haskell you can make sure that an argument belongs to two (or
>> however many) typeclasses.
>> doSomething :: (Ord a, Eq a, OtherTypeClass a) => a -> Int
>> But as I thought about this, I couldn't seem to think of a practical case
>> where this would be useful.
> This is quite useful, and it comes up all the time. I can't think of
> a particular example off the top of my head, but I know I have written
> lots of code with multiple type class constraints on the same type.
I know there are cases where this comes up in Haskell, but in certain
cases the needs arises only because of the way Haskell handles
typeclasses. What I mean is, if you had a function: something n =
[1..n], and your type was a -> [a], a needs to be declared as (Num a,
Enum a), even though this is sort of redundant, since you can't really
have a number that isn't also enumerable. I'm talking specifically
about cases where you'd have (TypeClass1 a, TypeClass2 a), and
TypeClass1 doesn't imply TypeClass2 or visa versa (Brandon's example
might have served that purpose, though I'm very new to Haskell and
haven't gotten a full grasp on monads yet).
> However, this isn't actually an advantage over Java, since Java
> supports multiple interface inheritance. You can just declare a new
> interface that extends several other interfaces, and then use that new
> interface instead of having to list multiple ones.
Ya but isn't that a pretty big hassle, especially assuming there are
many useful cases where you require a type to belong to multiple
typeclasses? In Java you'd have to declare a whole new interface for
every single different set of interfaces you want your arguments to
conform to, whereas in Haskell you can just specify them in a list. If
cases come up enough where you do need to make sure your arguments
conform to/belong to multiple interfaces/typeclasses, I'd see
Haskell's system as a pretty major improvement, though this is what
prompted me to ask the question in the first place, do such situations
come up enough for this to be as big a benefit as it might seem to be.
> I don't think I'm qualified to comment on the precise differences
> between interfaces and type classes, since I haven't thought about it
> all that much. Perhaps others can chime in with some differences.
> There is this page on the Haskell wiki:
Read through some of that (will probably read through more later), but
as I did it also occurred to me that I guess the typeclass system
might simply be a necessity to work with the rest of Haskell's type
system? Ie, the point of them isn't their benefits over interfaces,
it's that it's just the way things need to work in order to
effectively connect with algebraic types?
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners