<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class="">
<div><br class=""><blockquote type="cite" class=""><div class="">On 1. Oct 2017, at 22:43, <a href="mailto:davelist@mac.com" class="">davelist@mac.com</a> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><div class=""><br class=""><br class=""><blockquote type="cite" class="">On Oct 1, 2017, at 2:32 PM, Glenn L. Austin &lt;<a href="mailto:glenn@austinsoft.com" class="">glenn@austinsoft.com</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class=""><br class="">On Oct 1, 2017, at 8:56 AM, Dave Reed via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Sep 21, 2017, at 3:58 PM, V T via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class=""><br class="">Hi there!<br class=""><br class="">Is there a best way to check if a given type conforms to numeric protocol (Integer or FP) at runtime?<br class=""><br class="">func checkNumeric&lt;T&gt;(_ value: T) {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>/* return true if vaiue is Integer or FP */<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>/* this will not compile: */<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if value is Numeric {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class="">}<br class=""><br class="">Best regards!<br class=""><br class="">VT<br class=""><br class=""></blockquote><br class="">I think the way to do it is to try casting as the type, but you can't use "as? Numeric" as you get:<br class=""><br class="">error: protocol 'Numeric' can only be used as a generic constraint because it has Self or associated type requirements<br class=""><br class="">but you could check for each specific numeric type such as:<br class=""><br class="">func checkNumeric&lt;T&gt;(_ value: T) {<br class=""> &nbsp;&nbsp;if (value as? Int != nil) || (value as? Float != nil) {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("numeric")<br class=""> &nbsp;&nbsp;} else {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("not numeric")<br class=""> &nbsp;&nbsp;}<br class="">}<br class=""><br class="">checkNumeric(3)<br class="">checkNumeric(3.0)<br class="">checkNumeric("3")<br class=""></blockquote><br class="">You can also use the 'is' operator, as in 'value is Int || value is Float || value is Double'<br class=""><br class="">-- <br class="">Glenn L. Austin, Computer Wizard, <a href="http://AustinSoft.com" class="">AustinSoft.com</a><br class=""></blockquote><br class=""><br class="">Ah, I had forgotten "is" works in Swift<br class=""><br class="">Just be careful as:<br class=""><br class="">func checkNumeric&lt;T&gt;(_ value: T) {<br class=""> &nbsp;&nbsp;&nbsp;if (value is Int) || (value is Float) {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("numeric")<br class=""> &nbsp;&nbsp;&nbsp;} else {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("not numeric")<br class=""> &nbsp;&nbsp;&nbsp;}<br class="">}<br class=""><br class=""></div></div></div></blockquote><div><br class=""></div><div>Thanks all! My current implementation looks like this. A bit redundant, bat it works. My intention was to simplify that monster:</div><div><br class=""></div><div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class=""><span style="color: #95a8b6" class="">func</span> isNumber&lt;T&gt;(<span style="color: #95a8b6" class="">_</span> value: <span style="color: #a0afad" class="">T</span>) -&gt; <span style="color: #7b9a42" class="">Bool</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; <span style="color: #95a8b6" class="">let</span> valueMirror = <span style="color: #7b9a42" class="">Mirror</span>(reflecting: value)</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(202, 115, 53); background-color: rgb(41, 41, 41);" class=""><span style="color: #f4f4f4" class="">&nbsp; &nbsp; </span>#if<span style="color: #f4f4f4" class=""> </span>arch<span style="color: #f4f4f4" class="">(</span>arm<span style="color: #f4f4f4" class="">) || </span>arch<span style="color: #f4f4f4" class="">(</span>arm64<span style="color: #f4f4f4" class="">)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #95a8b6" class="">if</span> (valueMirror.subjectType == Int.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == UInt.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == Double.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == Int8.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == Int16.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == Int32.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == Int64.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == UInt8.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == UInt16.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == UInt32.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == UInt64.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == Float.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == Float32.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == NSNumber.<span style="color: #95a8b6" class="">self</span> || valueMirror.subjectType == NSDecimalNumber.<span style="color: #95a8b6" class="">self</span> ) {</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #95a8b6" class="">return</span> <span style="color: #95a8b6" class="">true</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #95a8b6" class="">else</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #95a8b6" class="">return</span> <span style="color: #95a8b6" class="">false</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(202, 115, 53); background-color: rgb(41, 41, 41);" class=""><span style="color: #f4f4f4" class="">&nbsp; &nbsp; </span>#else</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #95a8b6" class="">if</span> (valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Int</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">UInt</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Double</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Int8</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Int16</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Int32</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Int64</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">UInt8</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">UInt16</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">UInt32</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">UInt64</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Float</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Float32</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">Float80</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">NSNumber</span>.<span style="color: #95a8b6" class="">self</span> || valueMirror.<span style="color: #7b9a42" class="">subjectType</span> == <span style="color: #7b9a42" class="">NSDecimalNumber</span>.<span style="color: #95a8b6" class="">self</span> ) {</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #95a8b6" class="">return</span> <span style="color: #95a8b6" class="">true</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #95a8b6" class="">else</span> {</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #95a8b6" class="">return</span> <span style="color: #95a8b6" class="">false</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(202, 115, 53); background-color: rgb(41, 41, 41);" class=""><span style="color: #f4f4f4" class="">&nbsp; &nbsp; </span>#endif</div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(244, 244, 244); background-color: rgb(41, 41, 41);" class="">}</div><div class=""><br class=""></div></div><blockquote type="cite" class=""><div class=""><div class=""><div class="">checkNumeric(3)<br class="">checkNumeric(3.0)<br class="">checkNumeric("3")<br class=""><br class="">outputs:<br class=""><br class="">numeric<br class="">not numeric<br class="">not numeric<br class=""><br class="">Since the literal 3.0 is a Double so you'd have to catch every floating point type (including CGFloat, I suspect etc.) vs. just trying to cast to a Float (although I guess casting to a Float could have unexpected results also if someone made an extension that allows a type to be cast as a Float).<br class=""><br class="">Dave<br class=""><br class=""><br class=""></div></div></div></blockquote></div><br class=""></body></html>