A: Monoid
is a type parameter with a Context Bound. It's syntactic sugar.
The following:
def sum[A: Monoid](xs: List[A]): A
gets desugared into:
def sum[A](xs: List[A])(implicit val $ev: Monoid[A]): A
Question
In Learning Scalaz there is a type parameter on the method sum.
Does this mean that the type A is of type Monoid? However that seems wrong, if the type A is a Monoid then how can it also be an integer as it is used in the example. I must be reading the type parameter wrong.
What is the meaning of the type parameter? How should I be reading it?
def sum[A: Monoid](xs: List[A]): A = {
val m = implicitly[Monoid[A]]
xs.foldLeft(m.mzero)(m.mappend)
}
Solution
A: Monoid
is a type parameter with a Context Bound. It's syntactic sugar.
The following:
def sum[A: Monoid](xs: List[A]): A
gets desugared into:
def sum[A](xs: List[A])(implicit val $ev: Monoid[A]): A