<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 10, 2016, at 2:41 PM, Joe Groff &lt;<a href="mailto:jgroff@apple.com" class="">jgroff@apple.com</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 Nov 10, 2016, at 11:51 AM, Matthew Johnson &lt;<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" 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=""><br class="Apple-interchange-newline">On Nov 10, 2016, at 1:44 PM, Joe Groff &lt;<a href="mailto:jgroff@apple.com" class="">jgroff@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" 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="Apple-interchange-newline">On Nov 10, 2016, at 11:42 AM, Matthew Johnson &lt;<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Nov 10, 2016, at 1:34 PM, Joe Groff via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Nov 10, 2016, at 10:30 AM, Philippe Hausler &lt;<a href="mailto:phausler@apple.com" class="">phausler@apple.com</a>&gt; wrote:<br class=""><br class="">So I think there are a few rough edges here not just the hashing or equality. I think the issue comes down to the subclass of NSNumber that is being used - it is defeating not only hashing but also performance and allocation optimizations in Foundation.<br class=""><br class="">So what would we have to do to get rid of the “type preserving” NSNumber subclass?<br class=""></blockquote><br class="">The type-preserving subclasses remember the exact Swift type that a value was bridged from, to preserve type specificity of casts so that e.g. `0 as Any as AnyObject as Any as? Float` doesn't succeed even if the Any &lt;-&gt; AnyObject round-trip involves ObjC, thereby providing somewhat more consistent behavior between Darwin and Corelibs-based Swift. If we were willing to give that up, and say that NSNumbers just flat-out lose type info and can cast back to any Swift number type, then it seems to me we could use the pure Cocoa subclasses.<br class=""></blockquote><br class="">Would these only be value-preserving casts and return nil if information loss would occur? &nbsp;I think that might be preferable anyway. &nbsp;Maybe I’m just not thinking hard enough, but when would the original type information be important as long as information isn’t lost? &nbsp;When I interact with these casts and NSNumber (JSON parsing, etc) I generally *do not* want an attempted cast that would be value-preserving to ever fail.<br class=""></blockquote><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;"><span 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; float: none; display: inline !important;">I'm inclined to agree that the cast should be value-preserving rather than type-preserving. There was concern about the behavior being different on Darwin and Linux, which is why we try to be type-preserving so that pure Swift code that uses number values with Any or other polymorphic interfaces behaves consistently with Cocoa Foundation code that has to traffic in NSNumber for the same effect.</span><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></blockquote><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;">Are you saying that Swift on Darwin can’t have value-preserving behavior? &nbsp;It seems like I’m missing something here. &nbsp;If value-preserving is the desirable behavior can you elaborate on the specific challenges getting in the way of having that behavior everywhere?</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;">It would require a change to the type relationships between the number value types on Swift. They are currently plain, unrelated struct types, so you can't do something like:</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>let x: Int = 1</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>x as? Float // produces nil</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;">We could conceivably special-case the number types in Swift so that they do value-preserving casts, and maybe that's even a good idea, but we don't today.</div></div></blockquote><div><br class=""></div><div>What bothers me about the current behavior is that when you have a numeric value of type `Any` its casting behavior depends on how it was constructed. &nbsp;This makes it easy to write code that works with some numeric values of type Any and be badly broken for others. &nbsp;One can argue this is ok because the values have different types underlying types, but I think it turns out to be pretty confusing and problematic for numeric types in practice. &nbsp;</div><div><br class=""></div><div>This problem is compounded by the fact that *most* of the time when we work with opaque numeric values we’re working with actually working with values that do cast to all of the standard library numeric types, but aren’t necessarily value-preserving. &nbsp;They can truncate or overflow.</div><div><br class=""></div><div>To clarify what bothers me about the current behavior I’ll give an example:</div><div><br class=""></div><div>let&nbsp;json =&nbsp;"{ \"one\": 1, \"onePointZero\": 1.0, \"onePointOne\": 1.1,&nbsp;\"onePointNine\": 1.1, \"largerThanInt8Max\": 270 }"<br class="">let&nbsp;data =&nbsp;json.data(using: .utf8)!<br class=""><br class="">// casts always succeed, but might truncate or overflow<br class="">//let dict = try! JSONSerialization.jsonObject(with: data, options: []) as! [String:&nbsp;Any]<br class=""><br class="">// casts always succeed, but might truncate or overflow<br class="">//let dict: [String: Any] = ["one": 1 as NSNumber, "onePointZero": 1.0 as NSNumber,&nbsp;"onePointOne": 1.1 as NSNumber, "onePointNine": 1.9 as NSNumber, "largerThanInt8Max":&nbsp;270 as NSNumber]<br class=""><br class="">// casts always fail unless the target type matches the original type<br class="">let&nbsp;dict: [String:&nbsp;Any] = ["one":&nbsp;1,&nbsp;"onePointZero":&nbsp;1.0,&nbsp;"onePointOne":&nbsp;1.1,&nbsp;"onePointNine":&nbsp;1.9,&nbsp;"largerThanInt8Max":&nbsp;270]<br class=""><br class="">let&nbsp;oneAsDouble =&nbsp;dict["one"]&nbsp;as?&nbsp;Double&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// 1 or nil<br class="">let&nbsp;onePointZeroAsInt =&nbsp;dict["onePointZero"]&nbsp;as?&nbsp;Int&nbsp; &nbsp;// 1 or nil<br class="">let&nbsp;onePointOneAsInt =&nbsp;dict["onePointOne"]&nbsp;as?&nbsp;Int&nbsp; &nbsp;&nbsp;// 1 (truncated) or nil<br class="">let&nbsp;onePointNineAsInt =&nbsp;dict["onePointNine"]&nbsp;as?&nbsp;Int&nbsp; &nbsp;// 1 (truncated) or nil<br class="">let&nbsp;int8Overflow =&nbsp;dict["largerThanInt8Max"]&nbsp;as?&nbsp;Int8&nbsp; // 14 (overflow) or nil</div></div><div><br class=""></div><div>The net result of this is that it’s pretty hard to write correct code for numeric values of type Any. &nbsp;</div><div><br class=""></div><div>The truncation and overflow are particularly troublesome because this behavior is exhibited when dealing with JSON values from external sources whose behavior could change on us leading to garbage values rather than more immediate nil values.</div><div><br class=""></div><div>I would strongly prefer to see a single behavior that does not ever produce garbage values. &nbsp;It seems like value-preserving behavior is the only way to do that. &nbsp;That said, I have an open mind if there are other options. &nbsp;But I think we should try to do something better than we currently do.</div><div><br class=""></div><div>The current behavior is very subtle, taking a nontrivial amount of effort to understand despite the fact that it’s not at all obvious that there is anything that one should pay attention to at all (I would guess that most programmers will just expect value-preserving casts until they run into a bug). &nbsp;IMO this is not a good thing for a use case that applies to just about every app out there.</div><div><br class=""></div><div>Note: I’m only concerned with code that deals with numeric values of type Any here. &nbsp;If that requires a change in behavior to direct numeric type casts like your example I wouldn’t object, but I am not specifically asking for that.</div><div><br class=""></div><div>Matthew</div></body></html>