<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="">What I would like is a way to specialize a generic function in a block of code: (Strawman syntax)<div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>if T is Int.self {</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>//In this block of code T == Int</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}</div><div class=""><br class=""></div><div class="">It seems to me that the compiler might even be able to optimize this without a branch. &nbsp;If there is a return in the block, then it might even be able to throw away all the code after it for the Int version of the function.</div><div class=""><br class=""></div><div class="">Here is some actual (super ugly) code I wrote today. &nbsp;I am unhappy with it and looking to remove or improve it, but it is the best I can figure out at the moment:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;operation&lt;T&gt;(symbol:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, type:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.Type)-&gt;((</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)-&gt;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)?{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">if</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;==&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;op:((</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)-&gt;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)? =&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;symbol {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"+"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">: op = (+)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"-"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">: op = (-)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"*"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"x"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"•"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">: op = (*)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"/"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"÷"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">: op = (/)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">default</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">break</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">if</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;op !=&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;op&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">as</span><span style="font-variant-ligatures: no-common-ligatures;" class="">! ((</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)-&gt;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">// Function continues...</span></div><div class=""><br class=""></div><div class="">All this function does is take a string with a mathematical symbol in it and return an operation associated with that symbol/type (if it exists). &nbsp;In addition to the built in ones here, the user is able to register custom symbols and operations (the machinery for that is further down and less ugly).</div><div class=""><br class=""></div><div class="">Notice that I need to cast the operation back to (T,T)-&gt;T because it doesn’t know that T is an Int here. It has an error saying the cast always fails, but that isn’t true.</div><div class=""><br class=""></div><div class="">Ideally, I would be able to check if T is Numeric and then just return the operator. &nbsp;I have to do a bunch of extra work here to disambiguate for the compiler. It needs me to call out specific types (like Int) individually and then cast them back to T.</div><div class=""><br class=""></div><div class="">What I would like to do:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;operation&lt;T&gt;(symbol:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, type:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.Type)-&gt;((</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)-&gt;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)?{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">if</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;is Numeric</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;symbol {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"+"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;<span style="color: rgb(4, 51, 255);" class="">return</span>&nbsp;(+)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"-"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;<span style="color: rgb(4, 51, 255);" class="">return</span>&nbsp;(-)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"*"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"x"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"•"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;<span style="color: rgb(4, 51, 255);" class="">return</span>&nbsp;(*)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"/"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"÷"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;<span style="color: rgb(4, 51, 255);" class="">return</span>&nbsp;(/)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">default</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">break</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</span></div></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">// Function continues</span></span>…</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">or at least:</span></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><div style="font-family: Helvetica; font-size: 12px;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">static</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;operation&lt;T&gt;(symbol:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, type:</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.Type)-&gt;((</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)-&gt;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)?{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">if</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);" class="">T</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;is Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;symbol {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"+"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;<span style="color: rgb(4, 51, 255);" class="">return</span>&nbsp;(+)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"-"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;<span style="color: rgb(4, 51, 255);" class="">return</span>&nbsp;(-)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"*"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"x"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"•"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;<span style="color: rgb(4, 51, 255);" class="">return</span>&nbsp;(*)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"/"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(180, 38, 26);" class="">"÷"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;<span style="color: rgb(4, 51, 255);" class="">return</span>&nbsp;(/)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">default</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">break</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</span></div></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">// Function continues...</span></span></div></span></div><div class=""><br class=""></div><div class="">Is it something that could be possible in a future version of Swift? &nbsp;or am I missing something obvious that is much better than this?</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Feb 28, 2017, at 2:15 PM, Douglas Gregor via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Feb 28, 2017, at 2:00 PM, David Hart &lt;<a href="mailto:david@hartbit.com" class="">david@hartbit.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 28 Feb 2017, at 22:39, Douglas Gregor via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 27, 2017, at 11:21 PM, Nicolas Fezans via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">+1<div class="">I would also welcome to be able to use "or" and "and" logical operators (not only the not operator) on these constraints.</div></div></div></blockquote><div class=""><br class=""></div><div class="">You already have “and’ constraints: it’s what you get out of the comma-separated list of constraints in a where clause, or the “&amp;” composition syntax.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="">I have sometimes generic functions whose code is identical but is written twice: first with 'where T=P1' and then with 'where T=P2', being able to write for instance 'where T=(P1 or P2)' would be very handy IMO.</div><div class="">One could often argue that additional protocols and extensions could be defined as a workaround to the situation I just mentioned but it seems often a bit of an overkill to me when you only have a couple of functions with that combination of requirements.</div></div></div></blockquote><div class=""><br class=""></div><div class="">“Or” constraints are a nonstarter for me, because you can’t meaningfully type-check a generic function that uses “or” constraints: the problem goes exponential in the number of “or” constraints and the meaning of the function can change considerably depending on which set of terms are satisfied—in which case you have ambiguities again!</div><div class=""><br class=""></div><div class="">Whenever this topic comes up, I like to point people at:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2161.pdf" class="">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2161.pdf</a></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">Should we also follow Recommendation #2 and revert the<span class="Apple-converted-space">&nbsp;</span><b class="">P1 &amp; P2</b><span class="Apple-converted-space">&nbsp;</span>change to return to<span class="Apple-converted-space">&nbsp;</span><b class="">Any&lt;P1, P2&gt;</b><span class="Apple-converted-space">&nbsp;</span>:) Half-joking.</div></div></div></div></blockquote></div><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;"><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;">That line of argument got thoroughly shot down in the core team meeting when we discussed the introduction of the &amp; operator for types.</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;"><span class="Apple-tab-span" style="white-space: pre;">        </span>- Doug</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;"><br class=""></div><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; 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;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></body></html>