O'Reilly logo

Swift 3 Functional Programming by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mixing OOP and FP

So far, we have seen that adding FP capabilities to an OOP language leads to benefits in the OOP design.

In summary, OOP fits perfectly with FP when our objects are as immutable as possible. To make our objects as immutable as possible, we can consider the following principles:

  • Objects should be types that encapsulate related pieces of data
  • Objects can have methods; however, these methods shouldn't change the object and should instead return a new one of the appropriate type
  • All the required state data should be injected into the class's initialization so that it will be ready to use immediately
  • Static methods can be used freely and static variables should be avoided
  • Protocols and generics should be used to avoid code duplicates

These ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required