<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=""><div class="">[Apologies if this is in Swift 4 already.]</div><div class=""><br class=""></div><div class="">We can have generic parameters match a protocol. But I have an idea in my head that involves the type NOT conforming to a protocol. I don’t think we have a way currently to work with this.</div><div class=""><br class=""></div><div class="">In C++, templates can be specialized or partially specialized. Either specialization lets you define special code when the template parameters match the pattern. I realized that it can anti-match by putting your secret sauce in the general version of the template and leave the specialized version empty. But, AFAIK, we can’t specialize Swift generics (in the same way), so we’re stuck.</div><div class=""><br class=""></div><div class="">The change needs to be added to generic parameter conformance in parameter and where clauses. We should also support an anti-version for same-type requirements.</div><div class=""><br class=""></div><div class="">Now the question is what token (sequence) to use? For anti-conformance to same-type, I don’t see a reason to not use the obvious “!=“ choice. But what would be the opposite of the colon used in type/protocol matching? Would “!:” look dumb? Would we have to make sure a “T!” type expression isn’t against the “:” to prevent the parser from changing conformance to anti-conformance?</div><div class=""><br class=""></div><div class="">Would putting a “!” directly before the type/protocol to be matched (i.e. on the right of the colon) be better?</div><div class=""><br class=""></div><div class="">//=====</div><p style="margin: 0px; line-height: normal;" class="">Grammar of a Generic Parameter Clause</p>
<p style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">generic-parameter-clause</i> → <b class=""><</b> <i class="">generic-parameter-list</i> <b class="">></b></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">generic-parameter-list</i> → <i class="">generic-parameter</i> | <i class="">generic-parameter</i> <b class="">,</b> <i class="">generic-parameter-list</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">generic-parameter</i> → <i class="">type-name</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">generic-parameter</i> → <i class="">type-name</i> <b class="">:</b> <b class="">!</b>opt <i class="">type-identifier</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">generic-parameter</i> → <i class="">type-name</i> <b class="">:</b> <b class="">!</b>opt <i class="">protocol-composition-type</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">generic-where-clause</i> → <b class="">where</b> <i class="">requirement-list</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">requirement-list</i> → <i class="">requirement</i> | <i class="">requirement </i><b class="">,</b> <i class="">requirement-list</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">requirement</i> → <i class="">conformance-requirement</i> | <i class="">same-type-requirement</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">conformance-requirement</i> → <i class="">type-identifier</i> <b class="">:</b> <b class="">!</b>opt <i class="">type-identifier</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">conformance-requirement</i> → <i class="">type-identifier</i> <b class="">:</b> <b class="">!</b>opt <i class="">protocol-composition-type</i></p>
<p style="margin: 0px; line-height: normal;" class=""><i class="">same-type-requirement</i> → <i class="">type-identifier</i> <b class="">==</b> <i class="">type</i></p><div style="margin: 0px; line-height: normal;" class=""><i class="">same-type-requirement</i> → <i class="">type-identifier</i> !<b class="">=</b> <i class="">type</i></div><div class="">//=====</div><div class=""><br class=""></div><div class="">Where the fourth, fifth, ninth, and tenth productions add an optional “!”. And the last production is new (although with a misleading title).</div><div class=""><br class=""></div><div class="">…</div><div class=""><br class=""></div><div class="">Now reported at <<a href="https://bugs.swift.org/browse/SR-5588" class="">https://bugs.swift.org/browse/SR-5588</a>>.</div><div class=""><br class=""></div><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">— </div><div class="">Daryle Walker<br class="">Mac, Internet, and Video Game Junkie<br class="">darylew AT mac DOT com </div></div>
</div>
<br class=""></body></html>