DEV Community

Discussion on: Type Thinking 🤔 P2 - Typeclass

Collapse
 
iquardt profile image
Iven Marquardt

I want to challenge the Typescript part of your post. Hope that is ok.

Your Typescript example doesn't really use the generic type variable T. What it illustrates is subtype polymorphism, not ad-hoc. Typescript only supports a rather primitive form of ad-hoc polymorphism at runtime through function overloading. You can overload a single name with several function types and than provide the implementation, where each type must be represented in a corresponding if/else branch.

Type classes in Haskell are a compile time mechanism. They go beyond simple name overloading by giving the mapping of a name to several types itself a name. Now you can refer to this named mapping and assign several overloaded names to it. You can express laws its members must abide by. You can form hierarchies where a subclass inherits overloaded names from a superclass. Named mappings are type classes.

Collapse
 
lironn_h profile image
liron_hazan

Challenging is more than OK! my purpose of writing about those topics is "technical/personal grow"! The generic type just gives a certain agility.. I like comparing different languages to get a bit sharper on my daily lang and keep an interesting conversion :)