<div dir="ltr">Literals in Swift do not inherently have a type (at least conceptually). They are simply literals.<div><br></div><div>The compiler will interpret them as whatever ExpressibleBy_____Literal type is required to make the expression sensible.</div><div><br></div><div>There are default types which literals will become if no type information is available: a string literal becomes String, a floating point literal becomes Double, and so forth. But the literals themselves do not have a type, and will become whatever type makes sense in context.</div><div><br></div><div>Nevin</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 14, 2016 at 4:59 PM, Chris Anderson via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Thanks for that follow up, I’m still a little confused at why one direction works and the other does not, but I’m getting there.<div><br></div><div>I’ve found another issue I’ll bug report, but it’s along the same lines and wanted to run it by this thread. If I have an NSDecimalNumber, in Swift, and perform math on a literal value, (product) the code compiles. If I assign that value to a variable, or use any of the other Decimal/Double types, I cannot compile. I would expect a Double to not work, but I would expect ‘Decimal’ to work, in this case, as I’m not crossing the Objective C border. And, I’m confused how using the literal ‘2.0’ is interpreted as an NSDecimalNumber, and works in the ‘product’ stop, but I would expect the compiler to try and make it into a Double, as it does on the ‘test’ variable. </div><div><br></div><div> let value = NSDecimalNumber(value: 2)<br> let test = 2.0 // double<br> let product = value.multiplying(by: 2.0) // compiles<br> let x = value.multiplying(by: Decimal(<wbr>2.0)) // does not compile<br> let y = value.multiplying(by: Double(<wbr>2.0)) // does not compile<br> let z = value.multiplying(by: test) // does not compile</div><div><br></div><div>Best,</div><div>Chris Anderson</div><div><div class="h5"><div><br><div><blockquote type="cite"><div>On Nov 11, 2016, at 6:07 PM, Philippe Hausler <<a href="mailto:phausler@apple.com" target="_blank">phausler@apple.com</a>> wrote:</div><br class="m_-179216152419847767Apple-interchange-newline"><div><div style="word-wrap:break-word"><div><br></div><div>NSDecimal is not toll free bridged, but it does have a bridge to NSDecimalNumber.</div><div><br></div><div>So take this for example:</div><div><br></div><div>@objc class Exam: NSObject {<br> var grade: Double = 90.0<br>}</div><div><br></div><div>It would be reasonable to expect that is exposed in objc as:</div><div><br></div><div>@interface Exam : NSObject</div><div>@property double grade;</div><div>@end</div><div><br></div><div>and not:</div><div><br></div><div>@interface Exam : NSObject</div><div>@property NSNumber *grade;</div><div>@end</div><div><br></div><div>As it stands this is exposing as the structural type since that structural type comes from objective-c. Unlike String or Dictionary that have direct counterparts - NSDecimal and NSDecimalNumber both are sourced from the objective-c headers. That being said an API exposed in objc as returning a NSDecimalNumber should be exposed into swift as returning a Decimal (the struct NSDecimal). So if Exam was implemented in objc as such:</div><div><br></div><div>@interface Exam : NSObject</div><div>@property NSDecimalNumber *grade;</div><div>@end</div><div><br></div><div>that should be imported into swift as:</div><div><br></div><div>class Exam : NSObject {</div><div><span class="m_-179216152419847767Apple-tab-span" style="white-space:pre-wrap">        </span>var grade : Decimal</div><div>}</div><br><div><blockquote type="cite"><div>On Nov 11, 2016, at 2:58 PM, Adam C. Lickel via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_-179216152419847767Apple-interchange-newline"><div><div style="word-wrap:break-word"><div>NSDecimal has toll-free bridging with NSDecimalNumber so you can still do as casting when talking to an Objective-C API.</div><div><div><div><div><br><div><blockquote type="cite"><div>On Nov 11, 2016, at 2:56 PM, Chris Anderson via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_-179216152419847767Apple-interchange-newline"><div><div style="word-wrap:break-word"><div>Sure thing. Yeah, ideally the bridging would be fixed, but at the least, correcting the documentation will be a good start. Will file, thanks.</div><div><br></div><div>Best,</div><div>Chris Anderson</div><div><br></div><div><blockquote type="cite"><div>On Nov 11, 2016, at 5:55 PM, Tony Parker <<a href="mailto:anthony.parker@apple.com" target="_blank">anthony.parker@apple.com</a>> wrote:</div><br class="m_-179216152419847767Apple-interchange-newline"><div><div style="word-wrap:break-word">Hi Chris,<div><br></div><div>Can you file a radar or JIRA for us on this? It looks like something should be fixed in the documentation at least, or perhaps in the bridging.</div><div><br></div><div>- Tony</div><div><br><div><blockquote type="cite"><div>On Nov 11, 2016, at 1:46 PM, Chris Anderson via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_-179216152419847767Apple-interchange-newline"><div><div dir="ltr"><div style="font-family:helvetica;font-size:12px">I'm having problems with the type conversion between a Swift `Decimal` and an Objective C `NSDecimalNumber`.</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">If I have the Swift class:</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px"> @objc class Exam: NSObject {</div><div style="font-family:helvetica;font-size:12px"> var grade: Decimal = 90.0</div><div style="font-family:helvetica;font-size:12px"> }</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">And try to use that Swift class in Objective C, </div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px"> Exam *exam = [[Exam alloc] init];</div><div style="font-family:helvetica;font-size:12px"> NSDecimalNumber *result = [[NSDecimalNumber zero] decimalNumberByAdding:grade.<wbr>value];</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">I get the error:</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">Sending 'NSDecimal' to parameter of incompatible type 'NSDecimalNumber * _Nonnull'</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">as it seems like `grade` is being treated as an `NSDecimal` not an `NSDecimalNumber`. This seems incorrect as per <a href="https://developer.apple.com/reference/foundation/nsdecimalnumber" target="_blank">https://developer.apple.com/<wbr>reference/foundation/<wbr>nsdecimalnumber</a> it says </div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">"The Swift overlay to the Foundation framework provides the Decimal structure, which bridges to the NSDecimalNumber class. The Decimal value type offers the same functionality as the NSDecimalNumber reference type, and the two can be used interchangeably in Swift code that interacts with Objective-C APIs. This behavior is similar to how Swift bridges standard string, numeric, and collection types to their corresponding Foundation classes."</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">So I'm not sure if 1) I'm doing something wrong. 2) there's an error in the documentation or 3) this is a Swift bug. Number 1 on that list is definitely the most likely, but I wanted to see what I’m missing here.</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">I don't want to explicitly make the values in my Swift class `NSDecimalNumber` because then I cannot do simple arithmetic operations such as `+` without doing the whole ugly `decimalNumberByAdding` dance.</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">Thanks for the help!</div><div style="font-family:helvetica;font-size:12px"><br></div><div style="font-family:helvetica;font-size:12px">Best,</div><div style="font-family:helvetica;font-size:12px">Chris Anderson</div></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></div></blockquote></div><br></div>______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></div></div></div>______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div></div></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>