<div dir="ltr"><div>I think this is a very useful feature of Obj-C that I&#39;ve missed several times while writing Swift code.</div><div><br></div>However, calling it &quot;TheClass&lt;Protocol1, Protocol2&gt;&quot; directly conflicts with the syntax for specifying generic type parameters.<div><br></div><div>If we use &quot;protocol&lt;TheClass, Protocol1, Protocol2&gt;&quot; for this case, then `protocol` stops making sense. Maybe the keyword `class` could be allowed instead, like &quot;class&lt;TheClass, Protocol1, Protocol2&gt;&quot;.</div><div><br></div><div>Or, it might work to omit the keyword entirely, leaving only the angle brackets:</div><div><br></div><div><span style="font-family:monospace,monospace">class C {</span></div><div><span style="font-family:monospace,monospace">    weak var delegate: &lt;UIViewController, UICollectionViewDelegate&gt;?</span><br></div><div><span style="font-family:monospace,monospace">}</span></div><div><span style="font-family:monospace,monospace"><br></span></div><div><font face="monospace, monospace">if let task = object as? &lt;NSManagedObject, Task&gt; {</font></div><div><font face="monospace, monospace">    task.coreDataRelatedFunction()</font></div><div><font face="monospace, monospace">    task.taskRelatedFunction()</font></div><div><font face="monospace, monospace">}<br></font><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob Bandes-Storch<br></div></div></div></div>
<br><div class="gmail_quote">On Fri, Dec 4, 2015 at 3:07 PM, Daniel Muhra <span dir="ltr">&lt;<a href="mailto:daniel.muhra@gmail.com" target="_blank">daniel.muhra@gmail.com</a>&gt;</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>Currently it doesn’t seem possible to define a property or variable of a specific class that also implements one or more protocols.</div><div>Take as an example the following definition from Objective C:</div><div><br></div><div>NSManagedObject&lt;NSCoding, Task&gt;* someObject;</div><div><br></div><div>To my knowledge, currently it is only possible to define multiple protocols only:</div><div><br></div><div>var object : protocol&lt;NSCoding, Task&gt;</div><div><br></div><div>However, given the first example, sometimes it is not possible to restrict to protocols only. This becomes especially cumbersome, if you would need to cast down to such a construct within a single method and perform several steps which would require all those type information:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">func</span> doSomething(object: <span style="color:#703daa">AnyObject</span>) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> managedObject = object <span style="color:#bb2ca2">as</span>? <span style="color:#703daa">NSManagedObject</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#000000">        </span>// Do core data related stuff</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    <br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> task = object <span style="color:#bb2ca2">as</span>? <span style="color:#4f8187">Task</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#000000">        </span>// Do task related stuff</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }  </div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div><div><br></div><div>In this case you now have managedObject and task though you operate on the same object. Also it is not (easily) possible to interleave actions of those two.</div><div><br></div><div>My first idea here would be to introduce an Objective C like style of type naming. This also has the benefit, that it is perfectly clear, whether a type name refers to a class or a protocol (without first having to check it).</div><div>The downside, of course, is that protocol type variables would become something like Any&lt;MyProtocol&gt;</div><div><br></div><div>Any other ideas are welcome, but the initial problem still stands and should be addressed.</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=P-2BsYbBZHRBuLDBJaL4DIKDNfkkjpROowTyRAObV11qzACF9YRuxumYAeOcjMBMERlHbU2myf8ioaOsp-2BPToMuh895Axgj6LtwERWSlpL9aKsSMR1lHFMskcS5-2BhdmkamiQ8tBD07utFDtkrluR6H7OdaAICxJuPdmKgz8n6omnxYTdXv8RdTJVQEgc-2F2p-2F-2FGUi0XIRRWm8EroRMTRt4o-2FKAhh-2FsSuNkImzT5nJoaGNo-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
<br>_______________________________________________<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/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div></div>