Operator Overloading #846

Closed
ptarjan opened this Issue Nov 13, 2010 · 2 comments

Projects

None yet

3 participants

@ptarjan
ptarjan commented Nov 13, 2010

I'd love to use coffeescript as my 'go to' language choice, but having to worry if I'm hitting a number > 9e15 is a big pain point in Javascript.

I'd like to create a BigInteger class that would handle arbitrary precision integer operation. There are a few hooks I'd like:

  1. Let me override the base integer. Integer = require('./BigInteger')
  2. Allow operator overloading. 1 + 3 maps to (new BigInteger(1)).ADD(3), or 1 == 3 maps to (new BigInteger(1)).EQUALS(3).

Is there anything preventing coffeescript from supporting this?

@hen-zone

It's certainly possible, but without a static type system, it's not possible to overload operators selectively. Every single instance of ==, + etc, would have to be compiled into a function call, whether or not its operands were actually BigIntegers. The performance penalty would be crushing, and would apply to the entire program. Also, the overloaded operator definitions would need to explicitly deal with every JS type they might receive -- strings, booleans, objects, and native numbers, in addition to the custom type you wish to dispatch. Since types are unknown until runtime, the compiler would not be able to help.

Interoperability is another concern. Even if BigIntegers behaved exactly like real Number values within your code, they would still look like Objects to the runtime. You couldn't pass them to external JS libraries or to native methods who expected an actual number. Again, this is not something the compiler could remedy.

For more discussion, see #727.

@clyfe
clyfe commented Jan 5, 2011

Conclusion, for lost wanderers, on this issue:
(Jeremy's postion stated on freenode #coffeescript irc)

"I'm afraid we can't do it, because JavaScript doesn't allow it."

ie. It goes against CS philosophy "It's just JavaScript"

Maybe we'll have more luck upstream or downstream:

https://github.com/satyr/coco

https://github.com/fab13n/parsec-coffee-script

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment