<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>What are the advantages of using AnyProtocol over a simple `class` constraint? &nbsp;To my mind, If you're already using Any or AnyObject in an interface, it seems comfortable enough with dynamic dispatch that knowing the underlying representation of the thing you've got won't do you much good. &nbsp;</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">In your specific class, my thinking is a delegate need not be a protocol only if an Object presents just a good a target for a message.</div><div id="AppleMailSignature"><br>~Robert Widmann</div><div><br>2016/02/11 9:08、João Nunes via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; のメッセージ:<br><br></div><blockquote type="cite"><div>

<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style>
<div dir="ltr"><p style="font-size: 12px; line-height: normal; font-family: Helvetica;">Hello,</p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica;">I have been thinking about a case that is missing completely from swift.</p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica;">We have the AnyObject type to represent an Object of any type.&nbsp;</p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica;">I recently run upon a situation where i needed a type to represent any Protocol. A AnyProtocol Type would be useful for my case.</p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica;">Example:</p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica;">I have a generic class that needs to receive a protocol type. The code without the AnyProtocol is the following:</p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;">&nbsp;</p>
<p style="font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;"><br></p>
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse">
<tbody>
<tr>
<td colspan="2" valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span style="font-kerning: none; color: #a71d5d">public</span> <span style="font-kerning: none; color: #a71d5d">class</span> MulticastDelegate<span style="font-kerning: none; color: #a71d5d">&lt;</span>T<span style="font-kerning: none; color: #a71d5d">&gt;</span> {</p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><span class="Apple-tab-span" style="white-space:pre">        </span></p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-kerning: none; color: #a71d5d">private</span> <span style="font-kerning: none; color: #a71d5d">var</span> delegates <span style="font-kerning: none; color: #a71d5d">=</span> NSHashTable<span style="font-kerning: none; color: #a71d5d">.weak</span>ObjectsHashTable()</p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><span class="Apple-tab-span" style="white-space:pre">        </span></p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(167, 29, 93);"><span style="font-kerning: none; color: #323333"><span class="Apple-tab-span" style="white-space:pre">        </span></span>public<span style="font-kerning: none; color: #323333"> </span>init<span style="font-kerning: none; color: #323333">() {}</span></p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><span class="Apple-tab-span" style="white-space:pre">        </span></p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-kerning: none; color: #a71d5d">public</span> <span style="font-kerning: none; color: #a71d5d">func</span> <span style="font-kerning: none; color: #795da3">addDelegate</span>(delegate: T) {</p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">                </span><span style="font-kerning: none; color: #a71d5d">guard</span> delegate <span style="font-kerning: none; color: #a71d5d">is</span> <span style="font-kerning: none; color: #0086b3">AnyObject</span> <span style="font-kerning: none; color: #a71d5d">else</span> { <span style="font-kerning: none; color: #a71d5d">return</span> }</p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">                </span>delegates<span style="font-kerning: none; color: #a71d5d">.</span>addObject((delegate <span style="font-kerning: none; color: #a71d5d">as!</span> <span style="font-kerning: none; color: #0086b3">AnyObject</span>))</p>
</td>
</tr>
<tr>
<td valign="top" style="width: 50.0px; min-width: 50.0px; border-style: solid; border-width: 0.0px 1.0px 0.0px 0.0px; border-color: transparent #eeeeee transparent transparent; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
</td>
<td valign="top" style="width: 928.0px; padding: 0.0px 10.0px 0.0px 10.0px">
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">        </span>}</p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">}</p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><br></p></td></tr></tbody></table><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><br></p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><br></p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">This code needs to “trust” that T is a protocol and also make sure T is an Object.</p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><br></p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><br></p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">Example using AnyProtocol, if it existed:</p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><br></p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span style="font-kerning: none; color: #a71d5d">public</span> <span style="font-kerning: none; color: #a71d5d">class</span> MulticastDelegate<span style="font-kerning: none; color: #a71d5d">&lt;</span>T:AnyProtocol<span style="font-kerning: none; color: #a71d5d">&gt;</span> {</p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><span class="Apple-tab-span" style="white-space:pre">        </span></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-kerning: none; color: #a71d5d">private</span> <span style="font-kerning: none; color: #a71d5d">var</span> delegates <span style="font-kerning: none; color: #a71d5d">=</span> NSHashTable<span style="font-kerning: none; color: #a71d5d">.weak</span>ObjectsHashTable()</p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><span class="Apple-tab-span" style="white-space:pre">        </span></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(167, 29, 93);"><span style="font-kerning: none; color: #323333"><span class="Apple-tab-span" style="white-space:pre">        </span></span>public<span style="font-kerning: none; color: #323333"> </span>init<span style="font-kerning: none; color: #323333">() {}</span></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><span class="Apple-tab-span" style="white-space:pre">        </span></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">        </span><span style="font-kerning: none; color: #a71d5d">public</span> <span style="font-kerning: none; color: #a71d5d">func</span> <span style="font-kerning: none; color: #795da3">addDelegate</span>(delegate: T) {</p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">                </span>delegates<span style="font-kerning: none; color: #a71d5d">.</span>addObject(delegate)</p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><span class="Apple-tab-span" style="white-space:pre">        </span>}</p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">}</p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">Note that the protocol must be a class protocol. This could also be achieved with a where clause or AnyClassProtocol variant.</p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">This example is also available on github: <a href="https://github.com/jonasman/MulticastDelegate">https://github.com/jonasman/MulticastDelegate</a></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51); min-height: 14px;"><br></p>
<p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">To extend my proposal, there might be cases where AnyStruct and AnyEnum would be usefull. I have no examples for these cases, but i believe someone might have it.</p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><br></p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">What are your thoughts, should I open a PR proposal?</p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><br></p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);"><br></p><p style="font-size: 12px; line-height: normal; font-family: Menlo; color: rgb(50, 51, 51);">João Nunes</p>                                               </div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>