Comments (3)
Hi @bitbreakr, thanks for creating this issue. Functions on Money
objects always return a new Money
instance. This is why the third line of the example (fiveEur.subtract(new Money(470, Currencies.EUR));
) returns 0.30 EUR
(5.00 - 4.70
) and not 2.80 EUR
(7.50 - 4.70
). Though I agree it could perhaps be a bit clearer. I will add some clarifications in the example this weekend.
from ts-money.
@49e94b8f256530dc0d41f740dfe8a4c1, I can not speak to the authors reasoning; however here are some thoughts about immutable and mutable objects, and why a library might use immutable objects (especially one dealing with money where getting the value wrong can be very costly).
From my research, immutable objects
are especially applicable for value types, where objects don't have an identity so they can be easily replaced. And they can make concurrent programming way safer and cleaner (most of the notoriously hard to find concurrency bugs are ultimately caused by mutable state shared between threads).
- https://softwareengineering.stackexchange.com/a/151735
- https://frontstuff.io/how-to-handle-monetary-values-in-javascript#why-immutable
from ts-money.
@macor161
Any specific reason you went with immutable objects? Is there a disadvantage to allowing the modification of Money objects?
from ts-money.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-money.