Type classes explained

Polymorphism via inheritance

Polymorphism via type classes

OOP approach consolidates data and related functions in one place— class definition. “Type classes” go with a different approach — entities representing data are decoupled from entities responsible for implementation.

  1. Code duplication — if we want to change Circle we would also need to change CircleShape.
  2. To call areaOf we need to pass instance of CircleShape, not the Circle itself.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Oleksii Avramenko

Oleksii Avramenko

Into Scala, Haskell and functional programming