<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="">At which point does that happen? The O0 SIL for it has an alloc_stack $Point in main.<div class=""><br class=""></div><div class="">Otherwise, that's hard-to-predict copy elision at work anyway.<br class=""><div class=""><br class=""><div class="">
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Lucida Grande';  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">Félix</span>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Le 24 déc. 2015 à 14:40:13, John McCall &lt;<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div style="" class=""><blockquote type="cite" style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">On Dec 23, 2015, at 2:18 PM, Félix Cloutier via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">The "when exactly are copy constructors run" and "when is the compiler allowed to elide copies" parts are very important and not always obvious in current Swift code. For instance:<br class=""><br class=""><blockquote type="cite" class="">struct Point {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var x: Int<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var y: Int<br class="">}<br class=""><br class="">class Foo {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var point: Point<br class="">}<br class=""><br class="">let foo = Foo()<br class="">foo.point.x = 4<br class=""></blockquote><br class="">This code "expands" to:<br class=""><br class=""><blockquote type="cite" class="">var point = foo.point<br class="">point.x = 4<br class="">foo.point = point<br class=""></blockquote><br class="">in which you have two copies, whereas in C++ you would have none.<br class=""></blockquote><br style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">As a point of fact, no: when the class property is actually stored, the modifications are dynamically performed in place due to the materializeForSet optimization.</span><br style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">John.</span><br style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Menlo-Regular; font-size: 10px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="">Félix<br class=""><br class=""><blockquote type="cite" class="">Le 23 déc. 2015 à 16:25:50, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :<br class=""><br class="">Currently, in most places where this is desired, we treat the reference type's interface as part of the value's interface and use copy-on-write to manage the referenced storage, as with arrays, dictionaries, and sets. Eager copying by a C++-like mechanism is interesting, and we designed the runtime with an eye toward future C++ interop, but this still has pretty massive impacts on the rest of the language and implementation that need deeper consideration than "no impact on existing code". When exactly are copy constructors run? When, if ever, is the compiler allowed to elide copies? Is move optimization possible? Do you have destructors, and if so, when are they run? How and when do we know copying a shared value can be done in a thread-safe way? In addition to looking at the C++ model, I'd also recommend thinking about simply allowing custom retain/release of otherwise bitwise-identical copies, or something like D's "post-blit constructor", the design of which avoids some of the pitfalls of C++.<br class=""><br class="">-Joe<br class=""><br class=""><blockquote type="cite" class="">On Dec 23, 2015, at 1:03 PM, Charles Srstka via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Introduction:<br class=""><br class="">This is a request for a copy constructor mechanism for structs in Swift.<br class=""><br class="">Motivation:<br class=""><br class="">Suppose you have a class stored inside a struct, like so:<br class=""><br class="">class C {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>func copy() -&gt; C { … }<br class="">}<br class=""><br class="">struct S {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var i: Int<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var c: C<br class="">}<br class=""><br class="">and you create a couple of the structs, like so:<br class=""><br class="">let c = C()<br class="">let foo = S(i: 1, c: c)<br class="">var bar = foo<br class="">bar.i = 2<br class=""><br class="">Since the ‘bar’ variable was mutated, it now contains a copy of the original ‘foo’ struct. However, both structs still carry the same pointer to ‘c'. There may be cases where you would want a copy of the struct to make a copy of any reference types stored within; however, that does not seem to be possible currently.<br class=""><br class="">Proposed Solution:<br class=""><br class="">Adding a copy constructor to S that would be called when a copy of the struct is about to be made. This constructor would simply create a new instance, initialize it, and return it. The copy constructor would look like this:<br class=""><br class="">struct S {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var i: Int<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var c: C<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>copy {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><span class="Apple-tab-span" style="white-space: pre;">        </span>return S(i: self.i, c: self.c.copy())<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<br class="">}<br class=""><br class="">Structs that do not implement the copy constructor would get the same behavior as they do currently.<br class=""><br class="">Impact on Existing Code:<br class=""><br class="">There should be no impact on existing code that does not implement the copy constructor.<br class=""><br class="">Charles<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution</blockquote></div></div></blockquote></div><br class=""></div></div></body></html>