<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Aug 3, 2016 at 3:51 AM, Rick Mann via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span><br>
> On Aug 2, 2016, at 19:06 , Jordan Rose <<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>> wrote:<br>
><br>
> I don’t think it makes sense to do this. A protocol cannot control how a particular property is implemented (stored or computed), and any conforming type must initialize all of its stored properties before returning from its own initializer. (You can’t write an initializer in a protocol that doesn’t delegate to another initializer because you don’t know what other stored properties the conforming type might have.)<br>
><br>
> Given that the protocol can’t control how the property gets initialized, it doesn’t make sense to allow the protocol to "set the variable, but only in the initializer”.<br>
<br>
</span>Really? It seems pretty natural for a conforming type to set a property once in the initializer, and it's immutable from then on out. I can do that quite cleanly with classes, but there's no way (that I know) to describe this using protocols. Ideally, I could just do:<br>
<br>
protocol<br>
Element<br>
{<br>
let uuid: UUID<br>
}<br>
<br>
which implies that all conforming types must initialize that value on creation, or provide a getter with let semantics (the latter might be too easy to break, and could be disallowed, requiring conforming types to create storage for the property and set it in init()).</blockquote><div><br></div><div>The compiler only knows as much as you tell it, and when you define a protocol and set it as { get } only, it's not going to let you set that property in a protocol extension for a protocol that doesn't expect a setter. Why or how would the compiler let you?</div><div><br></div><div>The solution is to create your init implementation in a concrete type, and define the let property in there as well. E.g.</div><div><br></div><div><div>protocol Element {</div><div> var uuid: UUID { get }</div><div>}</div><div><br></div><div>class Item: Element {</div><div> let uuid: UUID</div><div><br></div><div> init(uuid: UUID) {</div><div> self.uuid = uuid</div><div> }</div><div>}</div></div><div><br></div></div></div></div>