iamyogish iamyogish - 5 months ago 71x
Swift Question

What happens when a Class conforms to a protocol, which contains mutating function?

I was just experimenting with Protocol programming in Swift. During this, I came across the following scenario.
Let's say we have a protocol like,

protocol SomeProtocol {
mutating func mutatingFunc()

Now let's say I have a class called MyClass and it conforms to my SomeProtocol like,

struct MyStruct {
var width = 0, height = 0

extension MyStruct: SomeProtocol {

//1. If I remove the mutating keyword from following method definition, compiler will give me error, that's understandable as we are modifying structure members
//2. Also note that, if I remove the mutating keyword while defining protocol & keep mutating keyword in the below method, the compiler will say that structure doesn't conform to protocol

mutating func mutatingMethod() {
width += 10
height += 10


class MyClass {
var width = 10

extension MyClass: SomeProtocol {

//1. However while implementing the protocol method in class, I can implement the same protocol method, and the compiler doesn't complaint even if I don't mention mutating keyword.
//2. But compiler will complain that mutating isn't valid on methods in classes or class-bound protocols, if I mention the mutating keyword
func mutatingMethod() {
width += 10

Let's say I have another structure & another Protocol like,

protocol AnotherProtocol {
func nonMutatingFunc()

struct MyAnotherStruct {
var width = 0
extension MyAnotherStruct: SomeProtocol, AnotherProtocol {
func mutatingFunc() {
//1. Compiler is happy even without the mutating keyword as we are not modifying structure members, also the structure conforms to SomeProtocol.
print("Hello World")
mutating func nonMutatingFunc() {
//1. Compiler cries that we are not conforming to AnotherProtocol as the function is mutating
width += 10


So now my observations are,

  1. For a class, it doesn't matter if the function is mentioned as mutating in the protocol and we are never allowed to mention methods in class as mutating.

  2. For a struct, if the implemented method, doesn't modify members we need not mention func as mutating even tough in protocol it is mutating.

  3. For a struct, if we implement protocol method as mutating func and if we don't specify the method as mutating func in the protocol. Compiler gives error.

Currently I am confused with the way the compiler is behaving with the structures due to the above scenarios. If someone could explain the significance of mutating functions inside a protocol i.e. "when we declare the func inside a protocol as a mutating, should we not make the func mutating when we implement it?" It'd be really great and helpful.

P.S. I know what mutating functions are, I am just confused with the mutating methods defined inside a protocol.

Thanks in advance.


I don't see what the "confusion" is. You've elucidated the rules beautifully! A struct may implement a protocol's mutating function as nonmutating, but it may not implement a protocol's nonmutating function as mutating. Once you've said those rules, there's nothing to be "confused" about. The rules are the rules.

Classes don't have mutating functions so your investigations there are sort of irrelevant. If you had declared your protocol as a class protocol, your mutating annotation in the protocol would have been illegal.