Wednesday, July 03, 2019

Is OOP compatible with FP?


Actually, OOP (Object Oriented Programming) was born out of Functional Programming and as such is still considered an advanced application of it.
Watch this 1986 MIT Computer Science class about the SICP book (Structured Interpretation of Computer Programs) and see how it covers OOP in Lisp, one of the oldest Functional Programming Languages out there:
OOP is basically illustrated as nothing other than smart data that knows its own operations automatically through polymorphism.
Example:
Complex numbers can have one of two representations:
  • Rectangular
  • Polar
Each representation has its own algorithms for basic algebraic operations:
  • Add
  • Subtract
  • Multiply
  • Divide
To avoid manually selecting the right algorithm function for each data representation, one may rely on typed data in Lisp so that the data representing complex numbers will know its own operations based on whether its internal representation type is rectangular or polar, and be able to dispatch the right function automatically via polymorphism.

No comments: