<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=""><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 1, 2017, at 8:56 AM, Dave Reed via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><blockquote type="cite" class="">On Sep 21, 2017, at 3:58 PM, V T via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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<T>(_ 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<T>(_ value: T) {<br class=""> if (value as? Int != nil) || (value as? Float != nil) {<br class=""> print("numeric")<br class=""> } else {<br class=""> print("not numeric")<br class=""> }<br class="">}<br class=""><br class="">checkNumeric(3)<br class="">checkNumeric(3.0)<br class="">checkNumeric("3")<br class=""></div></div></blockquote></div><br class=""></div><div class="">You can also use the 'is' operator, as in 'value is Int || value is Float || value is Double'<div class=""><br class=""><div class=""><div style="font-size: 11px;">-- <br class="">Glenn L. Austin, Computer Wizard, <a href="http://AustinSoft.com" class="">AustinSoft.com</a></div><div class=""><br class=""></div></div></div></div></body></html>