<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Phil,<div class=""><br class=""></div><div class="">I think the warning is bogus in this case. Do you mind filing a bug?</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 24, 2017, at 11:00 PM, Phil Kirby via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<title class=""></title>
<div class="">
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class="">Original StackOverflow post:
<div class=""><br class=""></div>
<div class=""><a href="https://stackoverflow.com/questions/46924554/redundant-superclass-constraint-in-swift-4" class="">https://stackoverflow.com/questions/46924554/redundant-superclass-constraint-in-swift-4</a><br class=""></div>
<div class=""><br class=""></div>
<div class="">
<table class="">
<tbody class="">
<tr class="">
<td class="votecell">
<div class="vote"><a class="star-off" href="https://stackoverflow.com/questions/46924554/redundant-superclass-constraint-in-swift-4#" title="Click to mark as favorite question (click again to undo)"></a>
<div class="favoritecount"><b class=""></b></div>
</div>
</td>
<td class="postcell">
<div class="">
<div class="post-text" itemprop="text"><p class="">I'm getting a <code class="">Redundant superclass constraint...</code> warning in swift4: (paste in a playground)</p>
<pre class="lang-swift prettyprint prettyprinted"><code class=""><span class="kwd">import</span><span class="pln"> </span><span class="typ">CoreData</span><span class="pln">
</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Item</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">NSManagedObject</span><span class="pln"> </span><span class="pun">{}</span><span class="pln">
</span><span class="kwd">protocol</span><span class="pln"> </span><span class="typ">DataSourceProtocol</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
associatedtype </span><span class="typ">DataSourceItem</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">NSManagedObject</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">protocol</span><span class="pln"> </span><span class="typ">DataSourceProtocolProvider</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span><span class="pln">
</span><span class="kwd">extension</span><span class="pln"> </span><span class="typ">DataSourceProtocolProvider</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">func</span><span class="pln"> createDataSource</span><span class="pun"><</span><span class="pln">T</span><span class="pun">:</span><span class="pln"> </span><span class="typ">DataSourceProtocol</span><span class="pun">>(</span><span class="pln">dataSource</span><span class="pun">:</span><span class="pln"> T</span><span class="pun">)</span><span class="pln">
where T</span><span class="pun">.</span><span class="typ">DataSourceItem</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="typ">Item</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code></pre><p class="">On the <code class="">createDataSource<T: DataSourceProtocol></code> declaration I get the following warning:</p><p class=""><code class="">Redundant superclass constraint 'T.DataSourceItem' : 'NSManagedObject'</code></p><p class="">I thought that you could specify that an associatedtype could be used with the <code class="">==</code> operator to constrain the associatedtype to a specific type. I want to have a func <code class="">createDataSource<T: DataSourceProtocol>(dataSource:T)</code> where the <code class="">DataSourceItem</code> is an <code class="">Item</code>.</p><p class="">If I replace the <code class="">==</code> operator with <code class="">:</code> then the warning goes away:</p>
<pre class="lang-swift prettyprint prettyprinted"><code class=""><span class="kwd">extension</span><span class="pln"> </span><span class="typ">DataSourceProtocolProvider</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">func</span><span class="pln"> createDataSource</span><span class="pun"><</span><span class="pln">T</span><span class="pun">:</span><span class="pln"> </span><span class="typ">DataSourceProtocol</span><span class="pun">>(</span><span class="pln">dataSource</span><span class="pun">:</span><span class="pln"> T</span><span class="pun">)</span><span class="pln">
where T</span><span class="pun">.</span><span class="typ">DataSourceItem</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">Item</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code></pre><p class="">This happens to be a completely different context now. This constraint specifies that I want to have a <code class="">func createDataSource<T: DataSourceProtocol>(dataSource:T)</code> where the <code class="">DataSourceItem</code> is a <em class="">subclass</em> of <code class="">Item</code>. Which isn't the same thing as <code class="">DataSourceItem</code> <em class="">is</em> an <code class="">Item</code> object. Also, the code runs fine with <code class="">==</code> so am I just not understanding how constraints work?</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class=""><br class="">
<div class=""></div>
</div>
</div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>