I'm reading a book with the following:
sealed trait Currency
case object USD extends Currency
... other currency types
case class Money(m: Map[Currency, BigDecimal]) {
... methods defined
}
The discussion goes on to recognize certain types of operations on Money
as being Monoidal so we want to create a Monoid
for Money
. What comes next though are listings I can't parse properly.
First is the definition of zeroMoney
. This is done as follows:
final val zeroMoney: Money = Money(Monoid[Map[Currency, BigDecimal]].zero)
What I have trouble following here is the part inside the Money
parameter list. Specifically the
Monoid[Map[Currency, BigDecimal]].zero
Is this supposed to construct something? So far in the discussion there hasn't been an implementation of the zero
function for Monoid[Map[A,B]]
so what does this mean?
Following this is the following:
implicit def MoneyAdditionMonoid = new Monoid[Money] {
val m = implicitly(Monoid[Map[Currency, BigDecimal]])
def zero = zeroMoney
def op(m1: Money, m2: Money) = Money(m.op(m1.m, m2.m))
}
The definition of op
is fine given everything else so that isn't a problem. But I still don't understand what zeroMoney
is given its definition. This also gives me the same problem with the implicit m
as well.
So, just what does Monoid[Map[Currency, BigDecimal]]
actually do? I don't see how it constructs anything since Monoid
is a trait with no implementation. How can it be used without defining op
and zero
first?