<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Can you do something like this?</p>
<p>func isNumber<T: Numeric>(_ value: T) -> Bool { return
true }</p>
<p>func isNumber<T>(_ value: T) -> Bool { return false }</p>
<p>I don't recall whether or not swift will pick the right version
of the function here, or whether this can even compile (mac isnt
open at the moment), but if you can overload functions in this way
then it might be much nicer than checking for lots of concrete
types.<br>
</p>
<br>
<div class="moz-cite-prefix">On 10/1/2017 6:27 PM, V T via
swift-users wrote:<br>
</div>
<blockquote type="cite"
cite="mid:8E8A3D8A-560B-4207-B3E0-0807C8C77197@mailbox.org">
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<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=""
moz-do-not-send="true">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 <<a
href="mailto:glenn@austinsoft.com" class=""
moz-do-not-send="true">glenn@austinsoft.com</a>>
wrote:<br class="">
<br class="">
<blockquote type="cite" class=""><br class="">
On Oct 1, 2017, at 8:56 AM, Dave Reed via
swift-users <<a
href="mailto:swift-users@swift.org" class=""
moz-do-not-send="true">swift-users@swift.org</a>>
wrote:<br class="">
<br 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=""
moz-do-not-send="true">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="">
</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=""
moz-do-not-send="true">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<T>(_ value: T) {<br class="">
if (value is Int) || (value is Float) {<br class="">
print("numeric")<br class="">
} else {<br class="">
print("not numeric")<br class="">
}<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<T>(<span style="color: #95a8b6" class="">_</span>
value: <span style="color: #a0afad" class="">T</span>)
-> <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=""> <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=""> </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=""> <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=""> <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=""> }</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="">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=""> <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=""> }</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=""> </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=""> <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=""> <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=""> }</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="">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=""> <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=""> }</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=""> </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="">
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
swift-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:swift-users@swift.org">swift-users@swift.org</a>
<a class="moz-txt-link-freetext" href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a>
</pre>
</blockquote>
<br>
</body>
</html>