<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Try this.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">protocol</span><span style="font-variant-ligatures:no-common-ligatures"> Position {</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> x: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Double</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">get</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">set</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">struct</span><span style="font-variant-ligatures:no-common-ligatures"> Car: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Position</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> x: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Double</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> move&lt;T&gt;(item: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">inout</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">T</span><span style="font-variant-ligatures:no-common-ligatures">) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">where</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">T</span><span style="font-variant-ligatures:no-common-ligatures">:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Position</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">    item.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">x</span><span style="font-variant-ligatures:no-common-ligatures"> += </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> car = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Car</span><span style="font-variant-ligatures:no-common-ligatures">(x: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">50</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">move</span><span style="font-variant-ligatures:no-common-ligatures">(item: &amp;</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">car</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,0,0);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">car</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">x</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">// 51</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p><p style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif" color="#000000">Zhaoxin</font></span></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 27, 2017 at 12:35 AM, Guillaume Lessard via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">In your example, the compiler needs a parameter of type Position. Car is a type of Position, but they are not interchangeable. See below:<br>
<span class=""><br>
&gt; On May 26, 2017, at 00:33, Седых Александр via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; protocol Position {<br>
&gt;     var x: Double { getset }<br>
&gt; }<br>
&gt;<br>
&gt; struct Car: Position {<br>
&gt;     var x: Double<br>
&gt; }<br>
&gt;<br>
&gt; func move(item: inout Position) {<br>
&gt;     item.x += 1<br>
&gt; }<br>
&gt;<br>
&gt; var car = Car(x: 50)<br>
<br>
</span>var pos: Position = car<br>
<br>
move(item: &amp;pos)    // this works.<br>
assert(pos.x == 51) // works<br>
<br>
The move function as you wrote it requires the memory representation of a Position variable, which Car does not have; when you assign it to a Position variable, the Car struct gets accessed through an indirection layer. (There was a WWDC talk about this last year or the year before.)<br>
<br>
You may want a generic function instead:<br>
<br>
func move&lt;P: Position&gt;(item: inout P) {<br>
  item.x += 1<br>
}<br>
<br>
move(item: &amp;car)    // this works, since it’s now calling the generic function.<br>
assert(car.x == 51) // works<br>
<br>
Cheers,<br>
Guillaume Lessard<br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</blockquote></div><br></div>