<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="" applecontenteditable="true"><div class="">I think you're missing the underlying semantic problem here:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">func transform(item: inout Position) {</div><div class=""> item = Airplane()</div><div class="">}</div><div class=""><br class=""></div><div class="">var car = Car(x: 50)</div><div class="">var pos: Position = car</div><div class="">move(item: &pos) // this works, but 'pos' is now an Airplane</div><div class="">move(item: &car) // this doesn't work because 'car' has to stay a Car</div></blockquote><div class=""><br class=""></div>Dave Abrahams likes to phrase this lesson as "generics preserve type information, while protocol values erase it". In this case your Car needs to stay a Car, so you need the type information to be preserved.<br class=""><div class=""><br class=""></div><div class="">Hope that helps,</div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On May 27, 2017, at 03:07, Zhao Xin via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default"><font face="georgia, serif" class="">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" class="">. The `</span><font face="georgia, serif" class="">T:Position` part restricts that the type conforms `Position`, but it won't use `</font><span style="font-family:georgia,serif" class="">Position`, it uses the type.</span></div><div class="gmail_default"><span style="font-family:georgia,serif" class=""><br class=""></span></div><div class="gmail_default"><font face="georgia, serif" class="">Zhaoxin</font></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, May 27, 2017 at 4:58 PM, Седых Александр via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="">Thanks. Why generic function don not require memory allocate for inout variable, even if it is protocol type?<br class=""><br class=""><br class=""><blockquote style="border-left:1px solid #0857a6;margin:10px;padding:0 0 0 10px" class="">
        Пятница, 26 мая 2017, 19:35 +03:00 от Guillaume Lessard <<a href="mailto:glessard@tffenterprises.com" target="_blank" class="">glessard@tffenterprises.com</a>>:<span class=""><br class="">
        <br class="">
        <div id="m_-3183879079503814274" class="">
        
<div class="m_-3183879079503814274js-helper m_-3183879079503814274js-readmsg-msg">
        
        <div class="">
                
                
<div id="m_-3183879079503814274style_14958165190000000972_BODY" class="">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 class="">
<br class="">
> On May 26, 2017, at 00:33, Седых Александр via swift-users <<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>> wrote:<br class="">
> <br class="">
> protocol Position {<br class="">
> var x: Double { getset }<br class="">
> }<br class="">
> <br class="">
> struct Car: Position {<br class="">
> var x: Double<br class="">
> }<br class="">
> <br class="">
> func move(item: inout Position) {<br class="">
> item.x += 1<br class="">
> }<br class="">
> <br class="">
> var car = Car(x: 50)<br class="">
<br class="">
var pos: Position = car<br class="">
<br class="">
move(item: &pos) // this works.<br class="">
assert(pos.x == 51) // works<br class="">
<br class="">
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 class="">
<br class="">
You may want a generic function instead:<br class="">
<br class="">
func move<P: Position>(item: inout P) {<br class="">
item.x += 1<br class="">
}<br class="">
<br class="">
move(item: &car) // this works, since it’s now calling the generic function.<br class="">
assert(car.x == 51) // works<br class="">
<br class="">
Cheers,<br class="">
Guillaume Lessard<br class="">
<br class="">
</div>
                
        </div>
        
</div>
</div>
</span></blockquote>
<br class=""></div>
<br class="">______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>