<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=""><div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jun 8, 2017, at 9:45 AM, Itai Ferber &lt;<a href="mailto:iferber@apple.com" class="">iferber@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Hi Gwendal,</span><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""><blockquote type="cite" class=""><div class="">On Jun 8, 2017, at 8:27 AM, Gwendal Roué via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class="Apple-interchange-newline">Le 8 juin 2017 à 16:51, James Froggatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :<br class=""><br class="">I've just been trying out the new Coding protocol, and was rather surprised when trying to implement the `encode(to encoder: Encoder)` method.<br class=""><br class="">The Swift evolution proposal provides the following example code:<br class=""><br class="">&nbsp;&nbsp;public func encode(to encoder: Encoder) throws {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Generic keyed encoder gives type-safe key access: cannot encode with keys of the wrong type.<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let container = encoder.container(keyedBy: CodingKeys.self)<br class=""><br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// The encoder is generic on the key -- free key autocompletion here.<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try container.encode(latitude, forKey: .latitude)<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try container.encode(longitude, forKey: .longitude)<br class="">&nbsp;&nbsp;}<br class=""><br class=""><br class="">Here, container is stored as a `let` value, and uses reference semantics, while the proposal also clearly lists these `encode` methods as mutating. With the current implementation of the proposal, the container must be stored as a `var`, which leads to code like the following:<br class=""><br class="">&nbsp;&nbsp;var container = encoder.singleValueContainer()<br class="">&nbsp;&nbsp;try container.encode(data)<br class=""></blockquote><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">Yes, practically speaking and with latest Swift 4, the container needs to be declared as `var`.</span><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">I admit it's weird, and feels unnatural:</span><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">&nbsp;&nbsp;public func encode(to encoder: Encoder) throws {</span><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// A mutated value that nobody consumes: so weird.</span><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var container = encoder.container(keyedBy: CodingKeys.self)</span><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try container.encode(latitude, forKey: .latitude)</span><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try container.encode(longitude, forKey: .longitude)</span><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">&nbsp;&nbsp;}</span><br class="" style="font-family: PragmataProMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></div></blockquote>Why? It’s perfectly reasonable for the container to maintain some internal state as it’s encoding. It shouldn’t have to sacrifice value semantics for that.</div></div></blockquote></div></div></div><br class=""></div>No big trouble, Itai: just that a value type is usually mutated before being used elsewhere.<div class=""><br class=""></div><div class="">Take this code snippet for example:<br class=""><div class=""><br class=""></div><div class=""><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var a = [1]</div></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>a.append(2)</div></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>// if a is not used any longer, something is wrong, don't you agree?</div><div class=""><br class=""></div><div class="">And now this other one:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var x = SomeValueType()</div></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>x.append(2)</div><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>// why would it be different for x?</div></div><div class=""><br class=""></div><div class="">One of the *possible* interpretations of value types is that they are (or look like, if you prefer) purely local, and without side effect.</div><div class=""><br class=""></div><div class="">I know that values can hold references and vice-versa, and that the exact distinction between value and reference types is thin, even generally in the hand of the library developer: one can expose a reference type that behaves like a value type, and one can write a value type that obviously hides some references (like the containers above).</div><div class=""><br class=""></div><div class="">That's all. No big deal, really.</div><div class="">Gwendal</div><div class=""><br class=""></div></div></body></html>