I used to make a lot of use of Java's BigDecimal for financial apps and was thinking of how useful it'd be, and how much nicer it'd be to use, in Dart, but the implementation brings up some interesting questions for the language regarding both ints and environment-specific code.
BigDecimals are usually implemented with a BigInteger value and an int scale. Dart's arbitrary-sized int would seem to make the BigInteger unnecessary so that you could define BigDecimal like:
class BigDecimal {
final int unscaledValue;
final int scale;
double get doubleValue() => unscaledValue / pow(scale, 10);
}
But we know this won't work in dart2js, which leaves a few options with no clear winner to me:
1) Leave the implementation as-is and let it fail in dart2js. This is clearly wrong to me, but not actually that much worse than the current int behavior.
2) Implement a BigInteger in Dart and use that. The problems are a) if we need a BigInteger why have an arbitrary sized int? Maybe int should just have undefined overflow after 32 bits to deal with JS. b) we now perform BigInteger operations in Dart rather than in the VM with a possible performance hit.
3) Implement a BigInteger, but only use it in dart2js, otherwise use int in the VM. Dart has no facility to do this for user-written code, so it'd have to be added. It might be quite useful for other purposes too.
4) Include BigDecimal in Dart, so that it can have environment-specific implementations with no change to the language.
This basically distills down to two questions: should int be arbitrarily sized, and should it be consistent across JS and VM?
Should there be some allowance for user-written environment-specific code?
I'd love to hear some thoughts on this.
Cheers,
Justin