<div dir="ltr">Unless you need to abstract what the Factory class does, I would eliminate AProto.<div><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz;min-height:16px"><span></span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="color:#bb2ca2">class</span><span> Factory {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>    </span><span style="color:#bb2ca2">func</span><span> constructInstance&lt;T: </span><span style="color:#4f8187">SimpleConstructableElement</span><span>&gt;(</span><span style="color:#bb2ca2">_</span><span> t: </span><span style="color:#4f8187">T</span><span>.Type) -&gt; </span><span style="color:#4f8187">T</span><span> {</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>        </span><span style="color:#bb2ca2">return</span><span> t.</span><span style="color:#bb2ca2">init</span><span>()</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>    }</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>}</span></p>
<p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz;min-height:16px"><br><span></span></p><p style="margin:0px;line-height:normal"><font face="arial, helvetica, sans-serif"><font color="#000000">If you truly want to genericize Factory, you can do the following:</font></font></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz;min-height:16px"><br><span></span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="color:#bb2ca2">class</span><span> Factory&lt;T: SimpleConstructableElement&gt;: </span><span style="color:#4f8187">AProto</span><span> {</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>    </span><span style="color:#bb2ca2">typealias</span><span> ElementType = </span><span style="color:#4f8187">T</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz;min-height:16px"><span></span><br></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>    </span><span style="color:#bb2ca2">func</span><span> constructInstance(</span><span style="color:#bb2ca2">_</span><span> t: </span><span style="color:#4f8187">T</span><span>.Type) -&gt; </span><span style="color:#4f8187">T</span><span> {</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>        </span><span style="color:#bb2ca2">return</span><span> t.</span><span style="color:#bb2ca2">init</span><span>()</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>    }</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>}</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz;min-height:16px"><br><span></span></p><p style="margin:0px;line-height:normal"><font face="arial, helvetica, sans-serif"><font color="#000000">However, ElementType can be inferred, so you can zap it, and then you get this:</font></font></p><p style="margin:0px;line-height:normal"><font face="arial, helvetica, sans-serif"><font color="#000000"><br></font></font></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span style="color:#bb2ca2">class</span><span> Factory&lt;T: SimpleConstructableElement&gt;: </span><span style="color:#4f8187">AProto</span><span> {</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>    </span><span style="color:#bb2ca2">func</span><span> constructInstance(</span><span style="color:#bb2ca2">_</span><span> t: </span><span style="color:#4f8187">T</span><span>.Type) -&gt; </span><span style="color:#4f8187">T</span><span> {</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>        </span><span style="color:#bb2ca2">return</span><span> t.</span><span style="color:#bb2ca2">init</span><span>()</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>    }</span></p><p style="margin:0px;line-height:normal">



</p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><span>}</span></p><p style="margin:0px;font-size:12px;line-height:normal;font-family:Inconsolata-dz"><br></p><p style="margin:0px;line-height:normal"><span><font color="#000000" face="arial, helvetica, sans-serif">…which compiles and I think does what you want it to. :)</font></span></p><p style="margin:0px;line-height:normal"><span><font color="#000000" face="arial, helvetica, sans-serif"><br></font></span></p><p style="margin:0px;line-height:normal"><span><font color="#000000" face="arial, helvetica, sans-serif">Dan</font></span></p></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 13, 2016 at 8:58 PM, Karl via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">So I’m trying to work generically to construct things based on their conformance to a protocol with an initialiser. I’m sure this worked before; I’ve done it before. Maybe there have been some changes in the language or something, because I’ve tried everywhich-way and this just isn’t flying:<br>
<br>
==================<br>
protocol AProto {<br>
<br>
        associatedtype ElementType<br>
        func constructInstance(_: ElementType.Type) -&gt; ElementType<br>
}<br>
<br>
protocol SimpleConstructableElement {<br>
        init()<br>
}<br>
<br>
extension Int : SimpleConstructableElement {}<br>
<br>
class Factory : AProto {<br>
<br>
        typealias ElementType = SimpleConstructableElement<br>
<br>
        func constructInstance(_ t: SimpleConstructableElement.Type) -&gt; SimpleConstructableElement {<br>
                return t.init()<br>
        }<br>
}<br>
<br>
Factory().constructInstance(Int.self)<br>
=================================<br>
<br>
/tmp/MyPlayground.playground:15:7: Type &#39;Factory&#39; does not conform to protocol &#39;AProto&#39;<br>
/tmp/MyPlayground.playground:6:7: Protocol requires function &#39;constructInstance&#39; with type &#39;(ElementType.Protocol) -&gt; ElementType&#39;<br>
/tmp/MyPlayground.playground:19:7: Candidate has non-matching type &#39;(SimpleConstructableElement.Type) -&gt; SimpleConstructableElement’<br>
<br>
=================================<br>
<br>
<br>
The thing that gets me about this is that Xcode’s code-completion tells me that SimpleConstructableElement.Type is of type SimpleConstructableElement.Protocol, so it should be the same as ElementType.Protocol and should be accepted, right?<br>
<br>
Karl<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div><br></div></div>