<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;">Original StackOverflow post:
<div><br /></div>
<div><a href="https://stackoverflow.com/questions/46924554/redundant-superclass-constraint-in-swift-4">https://stackoverflow.com/questions/46924554/redundant-superclass-constraint-in-swift-4</a><br /></div>
<div><br /></div>
<div>
<table>
<tbody>
<tr>
<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></b></div>
</div>
</td>
<td class="postcell">
<div>
<div class="post-text" itemprop="text">
<p>I'm getting a <code>Redundant superclass constraint...</code> warning in swift4: (paste in a playground)</p>
<pre class="lang-swift prettyprint prettyprinted"><code><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>On the <code>createDataSource<T: DataSourceProtocol></code> declaration I get the following warning:</p>
<p><code>Redundant superclass constraint 'T.DataSourceItem' : 'NSManagedObject'</code></p>
<p>I thought that you could specify that an associatedtype could be used with the <code>==</code> operator to constrain the associatedtype to a specific type. I want to have a func <code>createDataSource<T: DataSourceProtocol>(dataSource:T)</code> where the <code>DataSourceItem</code> is an <code>Item</code>.</p>
<p>If I replace the <code>==</code> operator with <code>:</code> then the warning goes away:</p>
<pre class="lang-swift prettyprint prettyprinted"><code><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>This happens to be a completely different context now. This constraint specifies that I want to have a <code>func createDataSource<T: DataSourceProtocol>(dataSource:T)</code> where the <code>DataSourceItem</code> is a <em>subclass</em> of <code>Item</code>. Which isn't the same thing as <code>DataSourceItem</code> <em>is</em> an <code>Item</code> object. Also, the code runs fine with <code>==</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;"><br />
<div></div>
</div>
</body>
</html>