1
我试图实现一个使用可遍历对象的函数。Traversable foldLeft的函数参数导致类型不匹配
def f[V, M[_] <: Traversable[_]](ei: M[V])(builder: GenericCompanion[M]): M[V] =
ei.foldLeft(builder.empty[V])((accum: M[V], el: V) => builder(el))
此代码不能编译,因为
Error:(22, 57) type mismatch;
found : el.type (with underlying type Any)
required: V
ei.foldLeft(builder.empty[V])((accum, el) => builder(el))
^
转载使用Scala 2.11.8和2.12.1。这很奇怪。为什么el
被假定为Any
?
我可以用猫库解决它。所以问题不是'如何去做'?但是“为什么纯粹的scala代码是错误的?”。
def f[V, M[_] : Foldable : Alternative](ei: M[V]): M[V] = {
val monoidK = implicitly[MonoidK[M]]
ei.foldLeft(monoidK.empty[V]) {
(accum, el) => monoidK.algebra[V].combine(accum, el.pure[M])
}
}
谢谢!我错过了类型变量绑定。 – Zernike