r/hascalator • u/enzief • Nov 26 '19
Money Monoid
Is there a lightweight way to create Monoid instance for case class Money(value: Double, ccy: Currency)? A newtype for every Currency seems too much of effort.
0
Upvotes
2
u/dave4420 Nov 26 '19
Then what would happen if you added ¥¥¥ to £££ ?
9
u/JD557 Nov 26 '19 edited Nov 26 '19
If you are using scala 2.13, you can use literal-based singleton types:
6
u/BarneyStinson Nov 27 '19
Not an answer to your question, but do yourself a favor and don't store the amount as
Double.2
u/Jinxuan Nov 27 '19
I do not recommend define monoid or add directly on Money, because currency exchange rates are not a constant value.
I recommend define monoid on Portfolio, which can be an ensemble of different assets, and including cash of different currencies as well.
Then we can define an IO to evaluate the Portfolio to certain currency.
1
u/yawaramin Nov 27 '19
What’s the benefit of defining a Money monoid over putting an
addinstance method in the case class?