[swift-corelibs-dev] NSDecimal & NSDecimalNumber

Dan Thorpe dan at blindingskies.com
Fri Dec 11 06:58:01 CST 2015

I’d be really interested in working on a Swift decimal number type. I’ve done some work on this for a Money type here: https://github.com/danthorpe/Money/tree/development/Money/Shared/Decimal

I agree that it’s best to start with NSDecimal, and that approach 2 would offer the most flexibility.

I have a few other thoughts regarding the number behaviors.

NSDecimal’s functions accept rounding mode parameters and return NSCalculationError but NSDecimalNumber accepts NSDecimalNumberBehavior parameter and throws NSExceptions. I’m not really sure what the reason for this discrepancy is and should a new Swift NSDecimal use NSDecimalNumberBehavior, but maintain backwards compatibility with default behaviors and defined rounding mode? 

Also, should a Swift NSDecimal continues with raising NSException, or adopt Swift style error handling? I don’t think we would want to introduce try/throw/catch semantics into the publicly exposed methods - but we could have an internal API layer which does. Backwards compatible functions would then have to convert between Swift ErrorType and NSException or NSCalculationError.


p.s. in the linked project, I made a DecimalNumberType protocol generic over the Behavior, which I’m not entire sure was a good decision or not.

> On 11 Dec 2015, at 12:32, Florian Reinhart via swift-corelibs-dev <swift-corelibs-dev at swift.org> wrote:
> Hi all,
> Is someone already working on NSDecimal and/or NSDecimalNumber? If not I would like to help and have a few questions on how to best tackle this.
> I think it’s best to start with NSDecimal and then use NSDecimal to create NSDecimalNumber. That’s why I want to concentrate on NSDecimal first. As far as I know there is no CF type we can utilize for NSDecimal and the existing interface is not Swift-like. I see two possible approaches here:
> We try to match the existing interface of NSDecimal as close as possible and only provide the same function interface with UnsafePointers all over the place
> We create a Swift-like version of the NSDecimal struct. This struct stores its data in private properties and exposes methods (add, multiply, etc.) and computed properties (isNaN, etc.). Additionally, to achieve compatibility with the existing Foundation NSDecimal, we provide the same functions that take NSDecimal pointers and internally use the methods of the Swift struct.
> What do you think?
> Unfortunately, I haven’t found a lot of documentation for NSDecimal, yet. Is there any documentation, which describes the NSDecimal functions?
> —Florian
> _______________________________________________
> swift-corelibs-dev mailing list
> swift-corelibs-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-corelibs-dev/attachments/20151211/9adbebda/attachment.html>

More information about the swift-corelibs-dev mailing list