Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing operator overloading in Javascript via a transpiler

One of the problems, for some of us, with Javascript is the lack of operator overloading. This makes writing numeric libraries awkward. For instance, we might want to write something like:

var a = new BigInteger(5);
var b = new BigInteger(10);
var c = a + b;

A possible solution is to transpile a language with operator overloading to Javascript. While feasible -- by replacing operators by function calls and type checks -- the consensus seems to be that this is impossible without killing performance. CoffeeScript has rejected the idea for this reason:

https://github.com/jashkenas/coffee-script/issues/846

But are there really no clever solutions?

For instance, it might be possible hoist type checks out of tight loops or to use some other pipeline where modern JS compilers can optimize away added cruft when the types are numeric.

Ideas?

like image 335
Tristan Avatar asked Sep 18 '11 18:09

Tristan


People also ask

Does JavaScript support operator overloading?

Note that you are free to overload any of the binary operators between your custom type and the JavaScript numeric types Number and BigInt . However, the only operators you are allowed to overload between your custom type and the String type are "==" and "<" .

Does TypeScript support operator overloading?

TypeScript provides the concept of function overloading. You can have multiple functions with the same name but different parameter types and return type. However, the number of parameters should be the same.

Can we override equivalence comparison in JavaScript?

I'm afraid you cannot achieve such thing in Javascript. See http://www.2ality.com/2011/12/fake-operator-overloading.html for some more details.


2 Answers

Are you really sure you need your big numbers to be useable by old functions written with normal numbers in mind (that use the traditional operators)? If you only need the overloading for your own convenience on functions you cave control over you might be able to get by by using a different, custom operator for bignums.

For example, you could write a compiler to safely convert

var d = a <+> b <*> c;

into

var d = (a).add((b).multiply(c));

or perhaps, if you want automatic conversions...

var d = toBignum(a).add(toBignum(b).multiply(toBignum(c)));

I don't really see you being able to force overloading on an existing implementation without a big hassle. While you could teoretically replace all occurences of + with <+> and so on, current Javascript implementations are not optimized for this and I don't even want to start thinking what would happen if you tried to pass a bignum to one of the native C++ functions that are under the hood.


edit: Not needing to override the base integer class is the important point here (note how in the link you gave, overriding is the first thing the guy wants...). I don't think you will be able to find some kind "magic" optimization though as you have no control over which of the many JS implementations is being used by the client.

If you really don't like a custom operator like <+> the only way to distinguish a normal + operator (you shoudln't meddle with) from a fancy + operator (you want to do bignum stuff with) would be forcing some kind of ad-hoc typing system on top of Javascript, perhaps via comments, custom syntax or (as mentioned in a comment) hungarian notation. Just settling for a custom operator name you hate less would be less hackish, IMO.

like image 96
hugomg Avatar answered Sep 29 '22 10:09

hugomg


Have a look how Scala implemented Operator Overloading:

They defined that every operator is a method call on an object, so your example would be:

var c = a["+"](b);

If you stop here, you could trivially implement method overload, the function would have to check the values passed by param. If you want to develop a better solution take Odersky's Programing in Scala book and some time to read all their ideas how they've solved the problem (which is a very nice solution!)

like image 21
Tobias P. Avatar answered Sep 29 '22 11:09

Tobias P.