mutating enum struct Swift | mobidevtalk, Learning through case study
Value type, ie enum
and struct
, has some very heavy weight presence on Swift. Swift community suggest to choose us the value type, enum struct, rather than the class type. So understanding the behavior of value type is always a great win. By construction value type is immutable on Swift. But what happens, when we use mutating
method on value type? This blog post is dedicated on the what and how of mutating keyword and the effect of it on enum and struct on the Swift domain.
Background
- Behavior of value type, enum struct, on Swift
- Introduction to
mutating
keyword - What does
mutating
keyword represent - Action points of mutating
- Am I doing something wrong
Behavior of value type, enum struct, On Swift
By default on Swift value type
thats struct
and enum
, are meant not to be modifiable, not the corresponding instance nor the instance’s property. We can say:
- No other instance even the own instance can not modify the value type
- The instance method can not modify the property
So what option Swift does provide us, when we need to mutate the value type
instance or instance’s property.
Introduction to mutating
keyword
Well Swift does provide us the mutation ability of value type
from its own instance method by preceding mutating
keyword on the instance-method’s signature.
But we can’t mutate any value type
instance outside of its own instance-method. So if we want to update some/any property of a value type
instance outside of its instance-method, the only way for us is to create a new instance of that value type
with the desire property-value.
On value types
ie struct
and enum
; when mutating
keyword is used before a method, it is symbolically saying that this method will modify the structure of struct
or enum
.
Let us talk with an example, say we have a stack T
type, T
is a generic type. And like any other stack we want to push and pop elements from the stack.
So if we have a value type
say struct
based stack, then to update the element we need to use mutating
keyword before push
and pop
, the two instance-method of Stack
.
import Foundationstruct stack<T>{
var elements: [T] = []
mutating func push(element: T){
elements.insert(element, at: 0)
}
mutating func pop() -> T? {
return elements.isEmpty ? nil : elements.remove(at: 0)
}
}
As we can see both the push
and pop
are marked with mutating
in order to modify the Stack
. Both are mutating the instance variable elements
inside their functional body.
What does mutating
keyword represent
The properties of value type
can not be changed from within its instance-method. So mutating
keyword tells the compiler that the following method will modify the properties of that value type
.
On the above example both the push(element: T)
and pop() -> T?
method are modifying an internal property, elements
, of the struct. So mutation
keyword was prefixed with those method.
action points of mutating
- all the changes made by the
mutating
method will be written on thevalue type
after that particular method ends. - the
mutating
method can completely write down a new instance to the implicitself
.
import Foundation struct Stack<T>{
mutating func reset(){ self = Stack()
// assigning new Stack to itself.
}
}
On the above example the reset
method is assigning itself a new Instance of its own type. And this new assigning will execute once the execution of the reset
is complete.
am I doing something wrong?
If we are using value type, enum
and struct
, as only the model then we should ask one question to ourself. Do we really want to mutate
the value type? As value type is immutable by nature.
So there must be something wrong on our design, either we are using value type
whether we should be using reference type
or may be we can create a new instance by providing default value for instance on convenience
init for value type
.
On the other hand, if we expand our understanding for value type to give it more responsibility other than only as Model type, then mutating probably very needed. As the first class citizen of Swift; value type, enum and struct, can really expand its operating area beyond only as Model type. So the talk about mutating on value type, enum and struct, on Swift is yet not finish. We will have some details talk on this topic when we talk about value type on some future talk. A log of talk 🤓.
Stay tunes. We will be back again.
Originally published at mobidevtalk.com on October 10, 2018.