<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="">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<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=""> </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=""> </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=""> ...</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=""> </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() {</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; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">check</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> {</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><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() {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 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; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #343893" class="">check</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">where</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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=""> </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() {</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> 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></body></html>