<div dir="ltr"><div class="gmail_default"><font face="georgia, serif">Because generic uses `Car` instead of `Position` when running the code, so there is no casting as `car as Position` as in your original code</font><span style="font-family:georgia,serif">. The `</span><font face="georgia, serif">T:Position` part restricts that the type conforms `Position`, but it won&#39;t use `</font><span style="font-family:georgia,serif">Position`, it uses the type.</span></div><div class="gmail_default"><span style="font-family:georgia,serif"><br></span></div><div class="gmail_default"><font face="georgia, serif">Zhaoxin</font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 27, 2017 at 4:58 PM, Седых Александр 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">
<div>Thanks. Why generic function don not require memory allocate for inout variable, even if it is protocol type?<br><br><br><blockquote style="border-left:1px solid #0857a6;margin:10px;padding:0 0 0 10px">
        Пятница, 26 мая 2017, 19:35 +03:00 от Guillaume Lessard &lt;<a href="mailto:glessard@tffenterprises.com" target="_blank">glessard@tffenterprises.com</a>&gt;:<span class=""><br>
        <br>
        <div id="m_-3183879079503814274">






















        












<div class="m_-3183879079503814274js-helper m_-3183879079503814274js-readmsg-msg">
        
         <div>
                
                
            <div id="m_-3183879079503814274style_14958165190000000972_BODY">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>
<br>
&gt; On May 26, 2017, at 00:33, Седых Александр via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">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>
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>
</div>
            
        
                
        </div>

        
</div>


</div>
</span></blockquote>
<br></div>
<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>
<br></blockquote></div><br></div>