<div dir="ltr">You'll need to specify a protocol where you're currently specifying "Bool". A generic constraint can't take a concrete type as an argument. So you could do this:<div><br></div><div>protocol BooleanType {}</div><div>extension Bool: BooleanType {}</div><div><br></div><div>extension foo where T: BooleanType {</div><div> ...</div><div>}</div><div><br></div><div>If you don't like this, you can define a protocol with an associatedtype and use a protocol extension to add this functionality to foo<T> instead. E.g.:</div><div><br></div><div>protocol bar {</div><div> associatedtype X</div><div>}</div><div><br></div><div>extension bar where X == Bool {</div><div> ...</div><div>}</div><div><br></div><div>public struct foo<T>: bar {</div><div> typealias X = T</div><div><br></div><div> ...</div><div>}</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 30, 2016 at 6:15 PM, Jens Alfke via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></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">I have a generic type, and I want to add some methods that are available when the type parameter is Bool. <b>(This is in Xcode 8 beta 6, btw.)</b><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:'Fira Code'"><span style="color:#005493">public</span><span> </span><span style="color:#005493">struct</span><span> foo<T> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:'Fira Code'"><span><span style="white-space:pre-wrap">        </span>...</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:'Fira Code'"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:'Fira Code';min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:'Fira Code'"><span style="color:#005493">extension</span><span> </span><span style="color:#703daa">foo</span><span> </span><span style="color:#005493">where</span><span> T: Bool {</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:'Fira Code'"><span><span style="white-space:pre-wrap">        </span>…</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:'Fira Code'"><span>}</span></div><div><span><br></span></div><div>The above fails to compile, with "type 'T' constrained to non-protocol type ‘Bool’”. Is that an error? The book doesn’t say that type constraints have to be to protocols; in fact the example in the section “Type Constraint Syntax” shows a type constraint that requires T to be a subclass of SomeClass.*</div><div><br></div><div>OK, so I’ll find a protocol for “boolean-like” values that’s implemented by Bool. The Swift reference for `Bool` in Xcode doesn’t list anything. <a href="http://Swiftdoc.org" target="_blank">Swiftdoc.org</a> shows a protocol `Boolean` that looks like exactly what I want, but my compiler’s never heard of it; is this something that snuck in post-3.0? If I switch <a href="http://Swiftdoc.org" target="_blank">Swiftdoc.org</a> back to Swift 2.2, it shows an equivalent protocol called `BooleanType`, but if I try to use that, the compiler says "'BooleanType' has been renamed to ‘Bool’”.</div><div><br></div><div>At this point I threw my laptop at the wall.</div><div>Not really, but I started to miss the stability and simplicity of C++… :-p</div><div><br></div><div>How the @$*% do I do this?</div><div><br></div><div>—Jens</div><div><br></div><div>* In fact the book never seems to talk about type constraints in generic type extensions. It covers each of the two concepts, but not how to use them together.</div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>