<div dir="ltr">Apologies- I did try finding existing documentation or discussion, but all I found was a stack overflow question asking how to do this.<div><br></div><div>The way I see it, it would be as simple as implicitly declaring protocols on a type when a type is declared: class implies ReferenceType, struct implies ValueType, etc.</div><div><br></div><div>In my opinion, this is very similar to the open/sealed argument about class inheritance. The API should be able to more strictly communicate the protocol usage, so it can provide more built-in functionality and better establish its contract with the consuming code.</div><div><br></div><div>I can see some interesting scenarios like-</div><div><br></div><div>// API can return success, but relies on dependency injection to communicate other results</div><div>protocol Result : EnumType {</div><div> case success</div><div>}</div><div><br></div><div>---</div><div>// serialization/communication frameworks </div><div>protocol Payload : ValueType, OnlyDataType {</div><div>}</div><div><br></div><div>class MyPayload : Payload { } // error</div><div><br></div><div>---</div><div><br></div><div>// this type is a collection of types and not a concrete type. it better indicates intent.</div><div>protocol ModuleDefinition : ProtocolType {</div><div> associatedtype View</div><div> associatedtype Model</div><div> // etc</div><div>}</div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 21, 2016 at 3:25 AM, Adrian Zubarev via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div class="m_-610772848823536449bloop_markdown"><p>We had similar talks about that topic before. There is a thread somewhere on this, but it’s mainly focused on value type semantics.</p>
<p>If I remember correctly we might even discussed to remove the <code>class</code> constraint completely from the language and introduce a protocol solution to this problem. </p>
<p>There are also some words about the <code>class</code> constraint in the <a href="https://github.com/apple/swift/blob/611fc78d28a5da97dd1bea40761b913b1077aef5/docs/GenericsManifesto.md#generalized-class-constraints" target="_blank">GenericsManifesto.md</a></p>
<blockquote>
<h3 id="m_-610772848823536449generalizedclassconstraints">Generalized <code>class</code> constraints</h3>
<p>The <code>class</code> constraint can currently only be used for defining protocols. We could generalize it to associated type and type parameter declarations, e.g.,</p>
<pre><code class="m_-610772848823536449Swift">protocol P {
associatedtype A : class
}
func foo<T : class>(t: T) { }
</code></pre>
<p>As part of this, the magical <code>AnyObject</code> protocol could be replaced with an existential with a class bound, so that it becomes a typealias:</p>
<pre><code class="m_-610772848823536449Swift">typealias AnyObject = protocol<class>
</code></pre>
<p>See the “Existentials” section, particularly “Generalized existentials”, for more information.</p>
</blockquote>
<ul>
<li><p>Instead of <code>: class</code> we might use <code>: AnyObject</code> as a constraint (or <code>: AnyReference</code>?).</p></li>
<li><p>Instead of <code>: struct/enum</code> we should have something like <code>: value</code> or even better <code>: AnyValue</code>.</p></li>
</ul>
<hr>
<p>I believe we should focus on existential types first before fixing this gap in Swift. <code>Any<T></code> might receive the mentioned constraints and could create something like this:</p>
<pre><code class="m_-610772848823536449swift">typealias AnyObject = Any<class>
typealias AnyValue = Any<value>
</code></pre><span class="HOEnZb"><font color="#888888">
<p></p></font></span></div><div class="m_-610772848823536449bloop_original_html"><span class="HOEnZb"><font color="#888888"><div id="m_-610772848823536449bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div> <br> <div id="m_-610772848823536449bloop_sign_1477037505090391040" class="m_-610772848823536449bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Adrian Zubarev<br>Sent with Airmail</div></div></font></span><div><div class="h5"> <br><p class="m_-610772848823536449airmail_on">Am 21. Oktober 2016 um 08:55:40, Xiaodi Wu via swift-evolution (<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>) schrieb:</p> <blockquote type="cite" class="m_-610772848823536449clean_bq"><span><div><div></div><div>
What's your use case for distinguishing structs and enums?<br>
<div class="gmail_quote">
<div dir="ltr">On Fri, Oct 21, 2016 at 1:40 AM Mike Kasianowicz via
swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>
wrote:<br></div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr" class="m_-610772848823536449gmail_msg">Currently protocols can have the
class constraint:
<div class="m_-610772848823536449gmail_msg">protocol MyProtocol : class {}<br class="m_-610772848823536449gmail_msg"></div>
<div class="m_-610772848823536449gmail_msg"><br class="m_-610772848823536449gmail_msg"></div>
<div class="m_-610772848823536449gmail_msg">It would be (a) intuitive and (b) useful to
allow such things as:</div>
<div class="m_-610772848823536449gmail_msg">protocol Model : struct {} or protocol Event
: enum {}</div>
<div class="m_-610772848823536449gmail_msg"><br class="m_-610772848823536449gmail_msg"></div>
<div class="m_-610772848823536449gmail_msg">These types of restrictions can help prevent
accidental anti-patterns or misuse of APIs.</div>
<div class="m_-610772848823536449gmail_msg"><br class="m_-610772848823536449gmail_msg"></div>
<div class="m_-610772848823536449gmail_msg">Seems simple and non-controversial...
right?</div>
<div class="m_-610772848823536449gmail_msg"><br class="m_-610772848823536449gmail_msg"></div>
<div class="m_-610772848823536449gmail_msg">[Note: I'd like to see even more
heavy-handed protocol restrictions in the future. For
example, a protocol describing an enum with a common case, or a
struct with no reference members. Great stuff for defensively
coding APIs.]</div>
</div>
______________________________<wbr>_________________<br class="m_-610772848823536449gmail_msg">
swift-evolution mailing list<br class="m_-610772848823536449gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="m_-610772848823536449gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="m_-610772848823536449gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="m_-610772848823536449gmail_msg" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br class="m_-610772848823536449gmail_msg">
</blockquote>
</div>
______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></div></span></blockquote></div></div></div><div class="m_-610772848823536449bloop_markdown"><p></p></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>