<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">This would be a bit counter-intutivie in my opinion, and it’s already possible with the language today. First of all, structs in Swift cannot be built upon. Rather, I believe the intention is to use protocols for such a task. That’s what the new Swift String and Substring structs do. The following code example demonstrates the intended behavior, without any additional language improvements.
<div><br /></div>
<div>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(225, 45, 160); background-color: rgb(41, 43, 54);"><br /></p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(225, 45, 160); background-color: rgb(41, 43, 54);">protocol <span style="color: #e7e8eb">Foo {</span></p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);">&#160; &#160; <span style="color: #e12da0">func</span> a() -&gt; <span style="color: #e12da0">Any</span>?</p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);">}</p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(41, 43, 54); min-height: 14px;"><br /></p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(225, 45, 160); background-color: rgb(41, 43, 54);">extension <span style="color: #18b5b1">Foo</span> <span style="color: #e7e8eb">{</span></p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);">&#160; &#160; <span style="color: #e12da0">func</span> a() -&gt; <span style="color: #e12da0">Any</span>? {</p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);">&#160; &#160; &#160; &#160; <span style="color: #e12da0">return</span> <span style="color: #e12da0">nil</span></p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);">&#160; &#160; }</p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);">}</p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(41, 43, 54); min-height: 14px;"><br /></p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);"><span style="color: #e12da0">struct</span> ValueSemantics: <span style="color: #18b5b1">Foo</span> {}</p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(41, 43, 54); min-height: 14px;"><br /></p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);"><span style="color: #e12da0">class</span> ReferenceSemantics: <span style="color: #18b5b1">Foo</span> {}</p>
<p style="margin: 0px; font-stretch: normal; font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(231, 232, 235); background-color: rgb(41, 43, 54);"><br /></p>
</div>
</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;"><br />
On Jun 21, 2017, 2:54 PM -0400, Mike Kluev via swift-evolution &lt;swift-evolution@swift.org&gt;, wrote:<br />
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;">
<div dir="ltr">sorry if this was already discussed.<br />
<div>
<div><br /></div>
<div>proposing an ability to derive a class from a struct or another value type (e.g. enum).</div>
<div>would make it easy to wrap value type in a reference type without explicit code:</div>
<div><br /></div>
<div>struct S {</div>
<div>&#160; &#160; var v: Int</div>
<div>&#160; &#160; func method() {}</div>
<div>&#160; &#160; static staticMethod() {}</div>
<div>}</div>
<div><br /></div>
<div>class C: S {</div>
<div>func otherMethod() {}</div>
<div>// ...</div>
<div>}</div>
<div><br /></div>
<div>let c = C()</div>
<div><br /></div>
<div>c.v = 1</div>
<div>c.method()</div>
<div>C.staticMethod()</div>
<div>c.otherMethod()</div>
<div><br /></div>
<div>shall work as if i had a struct variable and all struct functions implemented in a class calling through that var:</div>
<div><br /></div>
<div>pseudo code:</div>
<div><br /></div>
<div>class C {</div>
<div><br /></div>
<div>// auto generated internally:</div>
<div>&#160; &#160; var `struct`: S</div>
<div>&#160; &#160;&#160;</div>
<div>&#160; &#160; func method() {</div>
<div>&#160; &#160; &#160; &#160; `struct`.method()</div>
<div>&#160; &#160; }</div>
<div>&#160; &#160; static staticMethod() {</div>
<div>&#160; &#160; &#160; &#160; S.staticMethod()</div>
<div>&#160; &#160; }</div>
<div>//---</div>
<div>&#160; &#160;&#160;</div>
<div>func otherMethod() {}</div>
<div>}</div>
<div><br /></div>
<div>would also be nice to have ability to overload struct's methods:</div>
<div><br /></div>
<div>class C: S {</div>
<div>&#160; &#160; override func method() {</div>
<div>&#160; &#160; &#160; &#160; super.method()<br /></div>
<div>&#160; &#160; }<br /></div>
<div>}</div>
<div><br /></div>
<div>and have struct initializators inherited:</div>
<div><br /></div>
<div>let c = C(struct initialization params)</div>
<div><br /></div>
<div>thoughts?</div>
<div><br /></div>
<div>Mike</div>
</div>
<div><br /></div>
</div>
_______________________________________________<br />
swift-evolution mailing list<br />
swift-evolution@swift.org<br />
https://lists.swift.org/mailman/listinfo/swift-evolution<br /></blockquote>
</div>
</body>
</html>