4 ms·
This is so Javascript. It adds arbitrary precision integers (which I would love) in a way that seems to me to be guaranteed to be hard to use and bug-prone. T
by carapace 10y ago
This is so Javascript. It adds arbitrary precision integers (which I would love) in a way that seems to me to be guaranteed to be hard to use and bug-prone.
The 'n' suffix is clumsy as hell.
You can't mix Numbers and Integers? That's crazy.
23n + 2 is a TypeError even though "23" + 2 isn't?
- zackmorris 10y agoYa the ban on mixing stood out for me too. Javascript and many of its frameworks are too opinionated these days (attempting to govern the way users code) even though that's inherently not in their purview. Also I didn't see anything about the internal format of these numbers. Normally this shouldn't be a concern, but could affect memory usage. Giving this proposal a vote of no confidence, even though the need for arbitrary precision is high.
- hdhzy 10y ago> 23n + 2 is a TypeError even though "23" + 2 isn't? The former is banned because it's error prone the latter is allowed because of backwards compatibility.
- delinka 10y agoI'm going to need an explanation for how error prone it can be to add two numbers. Automatically promote the '2' to a '2n' and carry on. Where's the problem?
- deleted 10y ago[deleted]
- sillysaurus3 10y agoIf you do 23n + x, you probably want it to raise an error if x isn't a bigint. Otherwise you end up with more situations similar to NaNs appearing in unexpected places. It's arguable. There are at least two schools of thought, and both of them are valid. It just depends what you want to do. If you're using arbitrary precision, you're probably in a situation where it's better to be explicit than to have implicit coercion. For example, what should happen if you do 23n + 0.5? You can't have 23.5n, since it's not an integer. You could convert it back to a Number, but you can't do that if it's 9007199254740992n + 0.5. It's logical to throw an error, if a bit counterintuitive. Comparisons on the other hand probably shouldn't throw errors.
- delinka 10y agoI don't see this as any different than coercion with other pairs of types.
- sillysaurus3 10y agoI mean, sure, you can have that view. But again, what would you say should happen for 23n + 0.5? And for 9007199254740992n + 0.5? If you don't throw an error, you have to do something, and in those situations returning anything would be incorrect. You could return 23.5 in the former case, but then you'd no longer have an Integer. You could throw an error in the latter case, but that'd be a lot slower for JIT tracing compilers to execute than simply verifying that the types are the same.
- yladiz 10y agoTreat it as a arbitrarily large floating point value, rather than an integer.
- sillysaurus3 10y agoGood point, though in that case using arbitrary precision values anywhere in your program might tank performance. Execution speed still matters, especially now that we're at the physical limits of single cores, and automatically promoting to arbitrary floats seems like a poor trade of speed vs flexibility. It's way too tempting to "simply use arbitrary floats and be done with it", even in financial software, and then be in a situation where the entire program is slow. But it would be good to quantify how much slower it'd be. Animats pointed out a potentially more serious issue, which is that you can DoS a program by sending it an arbitrary precision number whose size is gigabytes. But that's true of any arbitrary precision number, not just floats.
- yladiz 10y agoI think in that case, internally it can handle smaller values differently than larger values (like Ruby). If the performance of large values is truly important, something using the FFI can be written to a program using something like C or Fortran.
- BerislavLopac 10y agoActually, I'd argue that the former is also because of backward compatibility: if it wasn't, it would be easy to treat all literal integers as Integers and everything else as Numbers, similarly to what exists in Python. I would dare say that even with this approach it would be possible to maintain backward compatibility with smart conversion, although I'm not familiar enough with ES internals to be more decided on that.
- hdhzy 9y agoPersonally I think JavaScript is already full of "smart" conversions and the trend now is to make new things simple instead of fueling XXI century's WTF.js (see http://brian.io/slides/dotjs-2012/ http://brian.io/slides/dotjs-2012/ ).
- koolba 10y agoAlso why "n"? Why not "i" or "d"?
- simplehuman 10y agoThey had to pick one.
- esrauch 10y agoN seems like a bad choice for Integer when the thing it isn't is Number.
- SamReidHughes 10y agoi is for sqrt(-1), d is for decimal, n is for "not not a bigint".
- deleted 10y ago[deleted]
- Udik 10y ago>This is so Javascript It's not, it's the exact opposite. Instead of doing type coercion, enforces strict typing. It's what people have asked for for ages, now they get it.
- JeremyBanks 10y agoThis is almost exactly the opposite of how classic JavaScript would have handled it. Your comment of "This is so Javascript" seems ignorant.