Skip to content

Commit e499447

Browse files
committed
Added operator overloading for Decimals
Note: This feature is being enabled through babel, and unfortunately doesn't really have any typescript support. Using an overloaded operator will show an error and be typed as "any". If ecmascript ever support operator overloading, then typescript will follow suit and these issues can be resolved. Here's the current proposal for how that could look like, although it's a long way's off from being accepted, if it ever is: https://github.com/tc39/proposal-operator-overloading Alternatively, there's a proposal for declaring that certain types have operator overloads, which would also work just perfectly: microsoft/TypeScript#42218 In the meantime, the errors will unfortunately remain present, although they won't cause any issues in production. BTW, the rhs can be any DecimalSource, but the lhs has to be a Decimal.
1 parent bc8622f commit e499447

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

babel.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module.exports = {
2-
presets: ["@vue/cli-plugin-babel/preset"]
2+
presets: ["@vue/cli-plugin-babel/preset"],
3+
plugins: [
4+
[
5+
"module:@jetblack/operator-overloading",
6+
{
7+
enabled: true
8+
}
9+
]
10+
]
311
};

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"devDependencies": {
2323
"@ivanv/vue-collapse-transition": "^1.0.2",
24+
"@jetblack/operator-overloading": "^0.2.0",
2425
"@types/lodash.clonedeep": "^4.5.6",
2526
"@typescript-eslint/eslint-plugin": "^4.18.0",
2627
"@typescript-eslint/parser": "^4.18.0",

src/lib/break_eternity.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,50 @@ export default class Decimal {
882882
return cost.div(currentRpS).add(cost.div(deltaRpS));
883883
}
884884

885+
public [Symbol.for('+')](other: DecimalSource): DecimalSource {
886+
return this.add(other);
887+
}
888+
889+
public [Symbol.for('-')](other: DecimalSource): DecimalSource {
890+
return this.sub(other);
891+
}
892+
893+
public [Symbol.for('*')](other: DecimalSource): DecimalSource {
894+
return this.times(other);
895+
}
896+
897+
public [Symbol.for('/')](other: DecimalSource): DecimalSource {
898+
return this.div(other);
899+
}
900+
901+
public [Symbol.for('minus')](): DecimalSource {
902+
return this.neg();
903+
}
904+
905+
public [Symbol.for('==')](other: DecimalSource): boolean {
906+
return this.eq(other);
907+
}
908+
909+
public [Symbol.for('>')](other: DecimalSource): boolean {
910+
return this.gt(other);
911+
}
912+
913+
public [Symbol.for('<')](other: DecimalSource): boolean {
914+
return this.lt(other);
915+
}
916+
917+
public [Symbol.for('>=')](other: DecimalSource): boolean {
918+
return this.gte(other);
919+
}
920+
921+
public [Symbol.for('<=')](other: DecimalSource): boolean {
922+
return this.lte(other);
923+
}
924+
925+
public [Symbol.for('!=')](other: DecimalSource): boolean {
926+
return this.neq(other);
927+
}
928+
885929
public normalize(): this {
886930
/*
887931
PSEUDOCODE:

0 commit comments

Comments
 (0)