O'Reilly logo

Swift Functional Programming - Second Edition 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

Side-effects and unintended consequences

Our mutable example's design can produce unpredictable side-effects. If multiple clients hold a reference to the ProductTracker instance, there are two ways for the products to change from underneath any of these clients:

  • We could simply reassign a value to the products directly. This is fixable by making it private for client calls but it is not fixable for in-class modifications.
  • We could call addNewProduct() from any client and modify the products.

Either way, there are going to be side-effects and unintended consequences because of the mutation.

In our immutable example, it is impossible to cause those unintended consequences because our FunctionalProductTracker is a value type and all the properties ...

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