3
我看了一下scalaz tutorial。Ad-hoc多态 - 类型
从this link,我明白下面的代码:
scala> def sum[A](xs: List[A])(implicit m: Monoid[A]): A = xs.foldLeft(m.mzero)(m.mappend)
sum: [A](xs: List[A])(implicit m: Monoid[A])A
scala> implicit val intMonoid = IntMonoid
intMonoid: IntMonoid.type = [email protected]
scala> sum(List(1, 2, 3, 4))
res9: Int = 10
但我不明白下面的代码:
scala> def sum[A: Monoid](xs: List[A]): A = {
val m = implicitly[Monoid[A]]
xs.foldLeft(m.mzero)(m.mappend)
}
sum: [A](xs: List[A])(implicit evidence$1: Monoid[A])A
scala> sum(List(1, 2, 3, 4))
res10: Int = 10
如果我们考虑List(1, 2, 3, 4)
,A
是Int
。
那么我们怎样才能有A
和Monoid
和A
和Int
在def sum[A: Monoid](xs: List[A]): A = {
?
谢谢