<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Bool isn’t a class (or protocol), so nothing can inherit from it.<div class=""><br class=""></div><div class="">Until Swift gains the ability to extend generic types where their parameters are *equal* to concrete types, as opposed to where they *inherit from or conform to* something, I think the best you can do is this:</div><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><div style="font-family: Inconsolata; color: rgb(88, 110, 117); margin: 0px; font-size: 14px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">check</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class=""> T: BooleanType {</span></div><div style="font-family: Inconsolata; color: rgb(88, 110, 117); margin: 0px; font-size: 14px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> isTrue()&nbsp;{&nbsp;</span>XCTAssertTrue(<span style="font-variant-ligatures: no-common-ligatures; color: rgb(41, 161, 152);" class="">actual</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 139, 210);" class="">boolValue</span>) }</div><div style="font-family: Inconsolata; color: rgb(88, 110, 117); margin: 0px; font-size: 14px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="font-family: Consolas;" class=""><br class=""></div><div class="">I *think* the feature you’re trying to use is on the todo list for Swift 3, but the last time I said that, I was completely wrong. The swift-evolution mailing list probably has more information.</div><div class=""><br class=""></div><div class="">Anyway, I hope that helps.</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div><div style="font-family: Consolas;" class=""><br class=""></div></div></div></div></div><div class=""><div><blockquote type="cite" class=""><div class="">On Apr 12, 2016, at 11:11 AM, Jens Alfke via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I’ve got a simple generic struct that wraps an instance of its parameter type.<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> check&lt;T&gt; {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> actual: </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">T</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> a: </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(128, 37, 127);" class="">actual</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = a</span>}</div></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class="">&nbsp; &nbsp; ...</div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div class=""><br class=""></div></span></div><div class="">Now I want to add a method that only works with a specific type, Bool:</div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> isTrue()&nbsp; &nbsp; {</span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">XCTAssertTrue</span><span style="font-variant-ligatures: no-common-ligatures" class="">(actual)}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">As I’d expect, the compiler doesn’t allow this: “Cannot convert value of type ’T’ to expected argument type ‘Bool’”.</div><div class="">So I’m trying to put this method in an extension that constrains T:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas; color: rgb(0, 84, 147);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">check</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&lt;</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&gt; {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 84, 147);" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 84, 147);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> isTrue()&nbsp; &nbsp; {XCTAssertTrue(actual)}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">This fails with "Constrained extension must be declared&nbsp;on the unspecialized generic type 'check' with constraints specified by a 'where’ clause”. OK, so I add a ‘where’ clause:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas; color: rgb(0, 84, 147);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">check</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">where</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> T: Bool {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 84, 147);" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 84, 147);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> isTrue()&nbsp; &nbsp; {</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 56, 147);" class="">XCTAssertTrue</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(actual)}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">This produces the error "type 'T' constrained to non-protocol type ‘Bool’”. This confuses me — why is constraining to a non-protocol type an error? <i class="">The Swift Programming Language</i>&nbsp;says “the ‘where’ clause … can express the constraints that a generic type T inherits from a class C”.</div><div class=""><br class=""></div><div class="">—Jens</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>