Copy constructors and lenses
After examining our immutable example implementation, we are not able to say that it covers all the functionalities of the imperative approach. For instance, it does not provide us with a way to change the producer of a product. After all, we cannot change it.
Whenever we need to change any property of the product
, we need to go through the following process:
let mexBananas = FunctionalProduct(name: bananas.name, price: bananas.price, quantity: bananas.quantity, producer: Producer(name: "XYZ", address: "New Mexico, Mexico"))
This solution is verbose and does not look nice. Let's examine how we can improve this process.
Copy constructors
The first solution is to provide a new init
method that copies the current instance. This approach is called a copy...