Swift - Protocol extensions - Property default values : <p>Let's say that I have the following protocol:</p>

<pre><code>protocol Identifiable {
    var id: Int {get}
    var name: String {get}
}
</code></pre>

<p>And that I have the following structs:</p>

<pre><code>struct A: Identifiable {
    var id: Int
    var name: String
}

struct B: Identifiable {
    var id: Int
    var name: String
}
</code></pre>

<p>As you can see, I had to 'conform' to the Identifiable protocol in struct A and struct B. But imagine if I had N more structs that needs to conform to this protocol... I don't want to 'copy/paste' the conformance (var id: Int, var name: String)</p>

<p>So I create a <strong>protocol extension</strong>:</p>

<pre><code>extension Identifiable {
    var id: Int {
        return 0
    }

    var name: String {
        return "default"
    }
}
</code></pre>

<p>With this extension now I can create a struct that conforms to the Identifiable protocol without having to implement both properties:</p>

<pre><code>struct C: Identifiable {

}
</code></pre>

<p>Now the problem is that I can't set a value to the id property or the name property:</p>

<pre><code>var c: C = C()
c.id = 12 // Cannot assign to property: 'id' is a get-only property
</code></pre>

<p>This happens because in the Identifiable protocol, id and name are only gettable. Now if I change the id and name properties to <strong>{get set}</strong> I get the following error:</p>

<p><em>Type 'C' does not conform to protocol 'Identifiable'</em></p>

<p>This error happens because I haven't implemented a setter in the protocol extension... So I change the protocol extension:</p>

<pre><code>extension Identifiable {
    var id: Int {
        get {
            return 0
        }

        set {

        }
    }

    var name: String {
        get {
            return "default"
        }

        set {

        }
    }
}
</code></pre>

<p>Now the error goes away but if I set a new value to id or name, it gets the default value (getter). Of course, <strong>the setter is empty</strong>.</p>

<p>My question is:
<strong>What piece of code do I have to put inside the setter?</strong> Because if I add <em>self.id = newValue</em> it crashes (recursive).</p>

<p>Thanks in advance.</p>
