<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 26. Jun 2017, at 13:44, Karl Wagner &lt;<a href="mailto:razielim@gmail.com" class="">razielim@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><blockquote type="cite" class="">On 25. Jun 2017, at 22:13, Robert Bennett &lt;<a href="mailto:rltbennett@icloud.com" class="">rltbennett@icloud.com</a>&gt; wrote:<br class=""><br class="">For a concrete type, a partial initializer is a function that:<br class="">Can set `let` instance variables<br class="">May not refer to `self` in an rvalue<br class="">Must set the same subset of instance variables regardless of the code path taken<br class=""><br class="">For a protocol, a partial initializer is simply a function that sets *any* subset of the instance variables. A protocol could provide a default implementation of one. In order for this `let` proposal to work, though, if a `let` variable is set in the protocol extension’s implementation of a (full) initializer, the protocol would need to provide at least one partial initializer that it does not provide an implementation for, so that a conforming type can fill in the gaps for all of the instance variables it defines that the protocol doesn’t require/know about. That way the compiler can have faith that a conforming type will be able to fully initialize itself with the default implementation of the required full initializer.<br class=""><br class=""></blockquote><br class="">I’ll say again: I think you’re approaching this from the wrong angle. You want stronger guarantees about mutability of properties, but the answer is not to make protocols be more restrictive about how conformers are written.<br class=""><br class="">What you are really asking for is the guarantee that some type which conforms to SomeProtocol has value-semantics. Once you have that guarantee, you know that you have a unique instance whose properties will only mutate if they are stored in a “var” property and you yourself mutate it.<br class=""><br class="">For example, an Array’s “count” property is technically a computed property, but in a “let”-declared Array you can treat it as a “let” constant because Arrays have value semantics. Your guarantees about properties only being set in the initialiser would automatically be fulfilled without the need of any additional alternate initialisation patterns.<br class=""><br class="">- Karl</div></div></blockquote></div><br class=""><div class="">Just to expand on this a little bit, as I see it there are two parts to your problem. First, you were asking for some kind of partial-initialisation. Second, you (or somebody) mentioned that there might be optimisation benefits.</div><div class=""><br class=""></div><div class="">Here’s a kind of partial initialisation you can use right now:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class=""><font face="Menlo" class="">public protocol SomeProtocol {<br class="">&nbsp; &nbsp; var neverReallyMutatesAfterInit: String { get }<br class="">}<br class=""><br class="">public extension SomeProtocol {<br class="">&nbsp; &nbsp; static func withProtocolDefaults() -&gt; SomeProtocol? {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; return (Self.self as? PartiallyInitializableSomeProtocol.Type).map { $0.withProtocolDefaults() }<br class="">&nbsp; &nbsp; }<br class="">}<br class=""><br class="">internal protocol PartiallyInitializableSomeProtocol: SomeProtocol {<br class=""><br class="">&nbsp; &nbsp; /// Creates an object with type-specific defaults.<br class="">&nbsp; &nbsp; init()<br class=""><br class="">&nbsp; &nbsp; /// Allows customisation by the protocol after init.<br class="">&nbsp; &nbsp; var neverReallyMutatesAfterInit: String { get set }<br class="">}<br class=""><br class="">internal extension PartiallyInitializableSomeProtocol {<br class="">&nbsp; &nbsp; &nbsp;static func withProtocolDefaults() -&gt; SomeProtocol {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; var newValue = Self()<br class="">&nbsp; &nbsp; &nbsp; &nbsp; newValue.neverReallyMutatesAfterInit = "ProtocolDefault"<br class="">&nbsp; &nbsp; &nbsp; &nbsp; return newValue<br class="">&nbsp; &nbsp; }<br class="">}<br class=""><br class="">public struct SomeConformer: PartiallyInitializableSomeProtocol {<br class="">&nbsp; &nbsp; public internal(set) var neverReallyMutatesAfterInit = "TypeDefault"<br class="">&nbsp; &nbsp; internal init() {}<br class="">}<br class=""><br class="">(SomeConformer.self as SomeProtocol.Type).withProtocolDefaults()?.neverReallyMutatesAfterInit // "ProtocolDefault"</font></div></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><br class=""></font></div></blockquote><div class="">With this example, the only way users of your library can obtain an instance is via the protocol function, which knows that it can construct a default instance and specialise the values.</div><div class=""><br class=""></div><div class="">As far as optimisation goes - I have to correct myself, because even in a protocol existential whose underlying type is known to have value semantics, computed properties themselves might not. For all the compiler knows, they could be reading/writing from global state. So the compiler can’t really eliminate repeat accesses unless it knows the underlying implementation (or we give it more information about how the content mutates). People have asked about marking stored/computed properties in protocols before, and I’ve always been against it. Protocols should contain abstract, semantic information with as little implementation-constraining stuff as possible whilst retaining the contract. That’s what makes them so powerful.</div><div class=""><br class=""></div><div class="">- Karl</div></body></html>