<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On 22 Jun 2017, at 22:28, Mike Kluev via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">On Wed, 21 Jun 2017 15:04:46 David Moore <mooredev at <a href="http://me.com/" class="">me.com</a>> wrote:</div><div class=""><br class=""></div><div class="">> This would be a bit counter-intutivie in my opinion, and it’s already possible </div><div class="">> with the language today. First of all, structs in Swift cannot be built upon. </div><div class="">> Rather, I believe the intention is to use protocols for such a task. That’s what </div><div class="">> the new Swift String and Substring structs do. The following code example </div><div class="">> demonstrates the intended behavior, without any additional language improvements.</div><div class="">> </div><div class="">> protocol Foo {</div><div class="">> func a() -> Any?</div><div class="">> }</div><div class="">> </div><div class="">> extension Foo {</div><div class="">> func a() -> Any? {</div><div class="">> return nil</div><div class="">> }</div><div class="">> }</div><div class="">> </div><div class="">> struct ValueSemantics: Foo {}</div><div class="">> </div><div class="">> class ReferenceSemantics: Foo {}</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">while you can use protocols, and protocol extensions specifically, to share implementation there are two obvious problems with this "workaround":</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">1) protocol extensions' based implementation is very limited as you have no storage around. e.g. start with a normal struct method that accesses an instant variable and try to refactor it into a protocol extension... in other words try doing anything useful other than "return nil" above, anything that requires instance variable access. you'll have to be creative in your protocol implementation, e.g. have a "var storage { get set }" as part of the protocol and implement that "var storage" inside your struct (and class), and in case of "struct" it would be strange as the struct itself is (already) the storage, so you would introduce another level of indirection for no good reason other than to satisfy this workaround, which is not nice to begin with and has to be thought upfront (see below)</div></div></div></blockquote><div><br class=""></div><div>Not sure what you mean by added indirection here, the following seems perfectly straightforward to me:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>protocol Foo {</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>var someValue:Int { get set }</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>func a() -> Any?</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><font face="Monaco" class=""><br class=""></font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>extension Foo {</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>func a() -> Any? { return self.someValue }</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><font face="Monaco" class=""><br class=""></font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>struct ValueSemantics:Foo { var someValue:Int }</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>class ReferenceSemantics:Foo {</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>var someValue:Int { return nil }</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div><br class=""></div><div>There is no added access overhead here, the only difference is that the protocol itself leaves it up to implementations whether someValue is stored or computed.</div><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="gmail_extra">2) the proposed method works with value types that are already available (e.g. the OS structs or third party ones) - something you can not change but still want to wrap into a reference type.</div></div></blockquote></div><br class=""><div class="">This sounds to me more like delegation support that's required, which is something that I would like to see for reducing boilerplate. But I'd prefer to see something like:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>struct Foo:SomeProtocol { var someValue:Int }</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>class Bar:SomeProtocol, SomeOtherProtocol {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>var foo:Foo implements SomeProtocol, SomeOtherProtocol.someMethod</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>// Implements all of SomeProtocol, some of SomeOtherProtocol</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><br class=""></div><div class="">Note that's not a specific proposal for syntax, the important point here is that I'm explicitly linking my stored property foo to implementation of a protocol, telling the compiler to automatically use foo's properties/methods to conform to SomeProtocol by default (but leaving me free to customise).</div><div class=""><br class=""></div><div class="">I don't think that masking the behaviour behind something that looks like extension is a good idea, for this reason I prefer explicit delegation. But like I say, I'm not 100% on my preferred syntax for it, I just think it should be its own, distinct feature rather than potentially fooling people into thinking they're extending a struct.</div></body></html>