Mutability

In some situations, we may actually want an object to be mutable. The immutability constraint can be removed easily by just adding the mutable keyword in front of the type definition. To make the Stock type mutable, we do the following:

mutable struct Stock <: Equity
symbol::String
name::String
end

Now let's try to update the name field in a hypothetical case where Apple changes its company name:

The name field has been updated as we wish. Note that, when a type is declared mutable, all of its fields become mutable. So, in this case, we would be allowed to change the symbol as well. Depending on the situation, such behavior may or may not be desirable. In Chapter 8, Robustness Patterns, we will go over some design patterns that we can use to build a more robust solution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.161.77