Zerobase Dev Blog

コードネームYentry

DartのintがJavaScriptのdoubleに変換されるのはどうかと思う

DartのコードはJavaScriptに変換できるわけですが、巨大な落とし穴が空いています。

Dartのintは、JSではdoubleに変換されてしまいます。かなり怪しげな問題です。バグの温床になると思います…

お金を扱うプログラム(をいまぼくは作ろうとしているわけですが)では致命的ですね。

解決策としては、Dart上で自前のbig int(というかcurrency的な)クラスを作ればよいと思っています。

どうせ最初からプリミティブ型を使うつもりはなくて、ドメイン・オブジェクトとしてラップするつもりだったので、まあ少し手間が増える程度(で済むといいなあ)かなと。適当な言語のbig int実装を移植すればいいかなと。

Integers can be arbitrarily large in Dart.

Note however, that when compiling to JavaScript, integers are implemented as JavaScript numbers. When compiling to JavaScript, integers are therefore restricted to 53 significant bits because all JavaScript numbers are double-precision floating point values. The behavior of the operators and methods in the int class therefore sometimes differs between the Dart VM and Dart code compiled to JavaScript.

int abstract class / dart:core Library / Dart API Reference