A babel plugin for operator overloading in JavaScript
A Babel plugin for operator overloading.
There is a trivial template project here.
This was based on an idea by Benjamin Fox
There are a number of great implementations of operator overloading including a current proposal. This proposal is far more sophisticated than this implementation, but I had issues with my use case. This is a very simple version that worked for me and might be a good work-around for you while we wait for the proposal to be accepted.
The following code adds two integers and then two points.
The directive at the start is required to enable the transformation.
'operator-overloading enabled'
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
[Symbol.for('+')](other) {
const x = this.x + other.x
const y = this.y + other.y
return new Point(x, y)
}
}
// Built in operators still work.
const x1 = 2
const x2 = 3
const x3 = x1 + x2
console.log(x3)
// Overridden operators work!
const p1 = new Point(5, 5)
const p2 = new Point(2, 3)
const p3 = p1 + p2
console.log(p3)
produces the following output:
5
Point { x: 7, y: 8 }
This is the first babel plugin I have written, so your mileage may vary.
I would appreciate any help!
~$ mkdir my-app
~$ cd my-app
~/my-app$ npm init -y
@babel/cli
for easier testing).~/my-app$ npm install --save-dev @babel/core @babel/preset-env @babel/cli
"node": "current"
is specified in targets. The targets specified must support arrow functions.~/my-app$ npm install --save-dev @jetblack/operator-overloading
.babelrc
file:{
"presets": [
[
"@babel/preset-env",
{
"targets" : {
"node": "current"
}
}
]
],
"plugins": ["module:@jetblack/operator-overloading"]
}
'operator-overloading enabled'
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
[Symbol.for('+')](other) {
const x = this.x + other.x
const y = this.y + other.y
return new Point(x, y)
}
}
const p1 = new Point(5, 5)
const p2 = new Point(2, 3)
const p3 = p1 + p2
console.log(p3)
babel-node
:~/my-app$ ./node_modules/.bin/babel-node.cmd index.js
Point { x: 7, y: 8 }
The plugin wraps expressions in arrow functions. The following is produced for x + y
.
(() => {
'operator-overloading disabled';
return x !== undefined && x !== null && x[Symbol.for("+")]
? x[Symbol.for("+")](y)
: x + y;
})()
For example the following code:
let x = 1, y = 2
let z = x + y
gets re-written as:
let x = 1, y = 2
let z = (() => {
return x !== undefined && x !== null && x[Symbol.for("+")]
? x[Symbol.for("+")](y)
: x + y;
})();
This allows the creation of custom overrides such as:
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
[Symbol.For('+')](other) {
const x = this.x + other.x
const y = this.y + other.y
return new Point(x, y)
}
}
As the plugin requires arrow functions the @babel/preset-env
preset targets
must support them.
Operator overloading is disabled for all files by default.
This can be enabled globally in the .babelrc
:
{
"presets": [
[
"@babel/preset-env",
{
"targets" : {
"node": "current"
}
}
]
],
"plugins": [
[
"@jetblack/operator-overloading",
{
"enabled": true
}
]
]
}
It can be enabled or disabled locally by including the 'operator-overloading'
directive at the start of a file or within a block.
'operator-overloading disabled'
let a = 1
let b = 2
let c = a + b
'operator-overloading enabled'
let x = Point(2, 4)
let y = Point(3, 5)
let z = x + y
Using operator overloading transpilation will increase compilation and run time when enabled.
No transpilation will be applied to he following operators.
typeof
===
!==
&&
||
instanceof
+
addition [Symbol.for('+')](other)
-
subtraction [Symbol.for('-')](other)
*
multiplication [Symbol.for('*')](other)
/
division [Symbol.for('/')](other)
%
remainder [Symbol.for('%')](other)
+
: plus [Symbol.for('plus')]()
-
: minus [Symbol.for('minus')]()
++
increment [Symbol.for('prefix-increment')]()
and [Symbol.for('postfix-increment')]()
--
decrement [Symbol.for('prefix-decrement')]()
and [Symbol.for('postfix-decrement')]()
Arithmetic assignment reuses the overrides for arithmetic.
+=
addition assignment [Symbol.for('+')](other)
-=
subtraction assignment [Symbol.for('-')](other)
*=
multiplication assignment [Symbol.for('*')](other)
/=
division assignment [Symbol.for('/')](other)
%=
remainder assignment [Symbol.for('%')](other)
&
bitwise and [Symbol.for('&')](other)
|
bitwise or [Symbol.for('|')](other)
~
bitwise not [Symbol.for('~')]()
^
bitwise xor [Symbol.for('^')](other)
<<
bitwise shift left [Symbol.for('<<')](other)
>>
bitwise sign propagating shift right [Symbol.for('>>')](other)
>>>
bitwise zero padding shift right [Symbol.for('>>>')](other)
Bitwise assignment reuses the overrides for bitwise.
&=
bitwise and [Symbol.for('&')](other)
|=
bitwise or [Symbol.for('|')](other)
~=
bitwise not [Symbol.for('~')]()
^=
bitwise xor [Symbol.for('^')](other)
<<=
bitwise shift left [Symbol.for('<<')](other)
>>=
bitwise sign propagating shift right [Symbol.for('>>')](other)
>>>=
bitwise zero padding shift right [Symbol.for('>>>')](other)
delete
delete property [Symbol.for('delete')](key)
in
has property [Symbol.for('in')](key)