[Haskell-cafe] Typeclasses for name punning (was: Re: I miss OO)
anotheraddress at gmx.de
Fri Nov 27 04:30:22 EST 2009
On Wednesday 25 November 2009 22:46:42 Luke Palmer wrote:
> I feel like this should be qualified. Type classes are not for name
> punning ; you wouldn't use a type class for the method bark on types
> Tree and Dog. But if you have a well-defined *structure* that many
> types follow, then a type class is how you capture that. It sounds
> like you do have this structure in your example.
> Further, with typeclasses, you can write methods that are generic over
> any type with that structure. So:
> class Temporal a where
> time :: a -> Time
> temporalOrder :: Temporal a => [a] -> [a]
> temporalOrder = sortBy (comparing time)
> The ability to do this indicates why using them for punning is a bad idea.
While I agree that this utility of typeclasses is lost if they are used for
name punning, I'm still unconvinced that it is actively *harmful* to use
classes for this lesser purpose :)
I think punning is a worthwhile goal on its own, since I find myself wasting
quite some thought on whether to prefix a record field name somehow, and if I
do, what I should use as a short but sufficiently unique prefix.
More information about the Haskell-Cafe